作者AmigoSafin ()
看板Python
標題[問題] 新手請益一題簡單的if statement
時間Mon Jun 3 01:46:45 2019
大家好
新手想請教一題簡單的題目
但不知為何我的答案總是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
推 iphone2003: 妳的迴圈跑完後,input會是'Y',所以在if-else那邊就 06/03 01:57
→ iphone2003: 把它轉成'Z'了 06/03 01:57
推 iphone2003: 題目要的應該是希望你用input()接收使用者輸入的字元 06/03 01:58
推 XperiaZ6C: 你的else多打一個=了 06/03 08:46
→ XperiaZ6C: 而且你不能用for迴圈,這樣input值永遠都是chr(90) 06/03 08:47
→ XperiaZ6C: 然後盡量不要用input來當變數名稱 06/03 08:48
→ tysh710320: 是永遠是chr(89)才對 你可以像一樓說的用輸入的方式 06/03 09:26
→ tysh710320: 或者用random.randint 來從這區間取一個整數 06/03 09:27
推 XperiaZ6C: 對也,是89,謝7樓糾正 06/03 09:38
推 jack85408540: 因為你的if else沒縮排 所以程式是先跑完for迴圈才 06/03 13:19
→ jack85408540: 進if判斷 06/03 13:19
→ tysh710320: 其實一樣,假如if,else縮排 print還是在外面只印出Z 06/03 13:25
→ tysh710320: 假如連print也縮排 變成印出B---Z(不會有A) 06/03 13:26
→ AmigoSafin: 謝謝大家的幫忙~~感謝:) 06/08 07:22