我一直在使用a子手模拟器进行学校评估,但是我似乎无法弄清楚为什么这段代码返回了,
AttributeError:“ str”对象没有属性“ isalhpa”
我不确定我是否正确使用isalpha
。有人可以帮助我吗?
这里是我的代码:
alpha = False
while alpha == False:
## Let the user guess a letter
letterGuess = input("Guess a letter: ")
## If it's longer or shorter than 1 then re-guess
if len(letterGuess) == 1:
alpha = letterGuess.isalhpa()
else:
print("Only guess one letter at a time")
我希望当字母猜测的长度为1且为字母字符时使alpha
为真
答案 0 :(得分:-1)
alpha = False
while alpha == False:
letterGuess = input("Guess a letter: ")
if len(letterGuess) == 1:
alpha = True
else:
print("Only guess one letter at a time")
现在,如果输入的长度为1,则它会跳出循环并使用alpha进行您需要的操作。
答案 1 :(得分:-1)
这是解决方案:
alpha = False
while alpha == False:
## Let the user guess a letter
letterGuess = input("Guess a letter: ")
## If it's longer or shorter than 1 then re-guess
if len(letterGuess) and letterGuess.isalpha()== 1:
alpha = True
else:
print("Only guess one letter at a time")
注意:切勿将学校作业放在这里。