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

作者: yeuan (心要夠堅定)   2019-06-04 09:40:14
原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
: 已經試了一兩小時都還沒找到原因
: 所以想請大家幫忙題點我一下
: 感恩
: 感謝~
作者: bibo9901 (function(){})()   2019-06-06 05:09:00
.....
作者: AmigoSafin   2019-06-08 07:21:00
謝謝大家的幫忙~

Links booklink

Contact Us: admin [ a t ] ucptt.com