作者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://webptt.com/cn.aspx?n=bbs/Python/M.1559497608.A.1D9.html
1F:推 iphone2003: 你的回圈跑完後,input会是'Y',所以在if-else那边就 06/03 01:57
2F:→ iphone2003: 把它转成'Z'了 06/03 01:57
3F:推 iphone2003: 题目要的应该是希望你用input()接收使用者输入的字元 06/03 01:58
4F:推 XperiaZ6C: 你的else多打一个=了 06/03 08:46
5F:→ XperiaZ6C: 而且你不能用for回圈,这样input值永远都是chr(90) 06/03 08:47
6F:→ XperiaZ6C: 然後尽量不要用input来当变数名称 06/03 08:48
7F:→ tysh710320: 是永远是chr(89)才对 你可以像一楼说的用输入的方式 06/03 09:26
8F:→ tysh710320: 或者用random.randint 来从这区间取一个整数 06/03 09:27
9F:推 XperiaZ6C: 对也,是89,谢7楼纠正 06/03 09:38
10F:推 jack85408540: 因为你的if else没缩排 所以程式是先跑完for回圈才 06/03 13:19
11F:→ jack85408540: 进if判断 06/03 13:19
12F:→ tysh710320: 其实一样,假如if,else缩排 print还是在外面只印出Z 06/03 13:25
13F:→ tysh710320: 假如连print也缩排 变成印出B---Z(不会有A) 06/03 13:26
14F:→ AmigoSafin: 谢谢大家的帮忙~~感谢:) 06/08 07:22