web*_*bbm 0 python string conditional
我正在尝试创建一个似乎不适用于我的Python代码的条件语句.我试图阅读并理解为什么,并且断言地说布鲁尔似乎没有缺陷.但是当我尝试将它们放入条件语句时,我会遇到语法错误.也许有人可以看看并检查我的代码在这个实例中失败的原因:
userinput = raw_input("Please enter a word: ")
if (len(userinput) > 1 and userinput.isalpha() == True):
print "Thanks for entering a word."
else:
print "That was not an acceptable word."
Run Code Online (Sandbox Code Playgroud)
具体来说,我收到一个语法错误,指出:
if len(userinput) > 1 and userinput.isalpha() == True):
SyntaxError: invalid syntax ^
Run Code Online (Sandbox Code Playgroud)
因为输入字符串会保留\n导致输入失败的字符:
考虑 userinput = raw_input("Please enter a word: ").strip()改为使用......
此外,isalpha()已经检查长度是否为一个或多个,将代码减少到:
if userinput.isalpha():
# whatever
Run Code Online (Sandbox Code Playgroud)
然后你的语法错误消失相当不错...(额外的)后userinput)
| 归档时间: |
|
| 查看次数: |
7112 次 |
| 最近记录: |