Re: [問題] 新手請益一題簡單的if statement

看板Python作者 (心要夠堅定)時間4年前 (2019/06/04 09:40), 4年前編輯推噓1(101)
留言2則, 2人參與, 4年前最新討論串2/2 (看更多)
原po的迴圈跑完只有存到ord(89) 也就是Y 所以答案永遠是Z 提供我的寫法 順便確保不能輸入A-Z以外的東西 check_lst = [chr(i) for i in range(65,91)] char = input('Enter an upper-case character: ') if char in check_lst: if char == 'Z': print('A') else: print(chr(ord(char)+1)) else: print('You need to enter an upper-case character.') ※ 引述《AmigoSafin ()》之銘言: : 大家好 : 新手想請教一題簡單的題目 : 但不知為何我的答案總是Z : 似乎沒有成功loop : 還請大家賜教 感謝! : 題目: : Write a program that takes a character as input (a string of length 1), which : you should assume is an upper-case character; the output should be the next : character in the alphabet. If the input is 'Z', your output should be 'A'. : (You will need to use an if statement. ) : 我的code: : for i in range(65,90): : input=chr(i) : if input!="Z": : output=chr(ord(input)+1) : else: : output=="A" : print (output) : 註:因為65-90就是A到Z的index : 但我的code是不論他給我input為多少 : 我的program output都是Z : 已經試了一兩小時都還沒找到原因 : 所以想請大家幫忙題點我一下 : 感恩 : 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 67.249.65.90 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1559612416.A.6B3.html ※ 編輯: yeuan (67.249.65.90 美國), 06/04/2019 09:41:37

06/06 05:09, 4年前 , 1F
.....
06/06 05:09, 1F

06/08 07:21, 4年前 , 2F
謝謝大家的幫忙~
06/08 07:21, 2F
文章代碼(AID): #1SzSm0Qp (Python)
文章代碼(AID): #1SzSm0Qp (Python)