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

看板Python作者時間5年前 (2019/06/03 01:46), 編輯推噓5(509)
留言14則, 5人參與, 5年前最新討論串1/2 (看更多)
大家好 新手想請教一題簡單的題目 但不知為何我的答案總是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), 來自: 68.180.87.229 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1559497608.A.1D9.html

06/03 01:57, 5年前 , 1F
妳的迴圈跑完後,input會是'Y',所以在if-else那邊就
06/03 01:57, 1F

06/03 01:57, 5年前 , 2F
把它轉成'Z'了
06/03 01:57, 2F

06/03 01:58, 5年前 , 3F
題目要的應該是希望你用input()接收使用者輸入的字元
06/03 01:58, 3F

06/03 08:46, 5年前 , 4F
你的else多打一個=了
06/03 08:46, 4F

06/03 08:47, 5年前 , 5F
而且你不能用for迴圈,這樣input值永遠都是chr(90)
06/03 08:47, 5F

06/03 08:48, 5年前 , 6F
然後盡量不要用input來當變數名稱
06/03 08:48, 6F

06/03 09:26, 5年前 , 7F
是永遠是chr(89)才對 你可以像一樓說的用輸入的方式
06/03 09:26, 7F

06/03 09:27, 5年前 , 8F
或者用random.randint 來從這區間取一個整數
06/03 09:27, 8F

06/03 09:38, 5年前 , 9F
對也,是89,謝7樓糾正
06/03 09:38, 9F

06/03 13:19, 5年前 , 10F
因為你的if else沒縮排 所以程式是先跑完for迴圈才
06/03 13:19, 10F

06/03 13:19, 5年前 , 11F
進if判斷
06/03 13:19, 11F

06/03 13:25, 5年前 , 12F
其實一樣,假如if,else縮排 print還是在外面只印出Z
06/03 13:25, 12F

06/03 13:26, 5年前 , 13F
假如連print也縮排 變成印出B---Z(不會有A)
06/03 13:26, 13F

06/08 07:22, 5年前 , 14F
謝謝大家的幫忙~~感謝:)
06/08 07:22, 14F
文章代碼(AID): #1Sz0k87P (Python)
文章代碼(AID): #1Sz0k87P (Python)