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

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

Links booklink

Contact Us: admin [ a t ] ucptt.com