Kel*_*har 0 python runtime-error conditional-statements
我的python代码中的条件有问题.这是一个数学应用程序,这里的代码部分运行不正常:
def askNumber():
"""Asks the number to test"""
a=raw_input("Select the number to test (type 'exit' for leaving):")
if len(a)!=0 and a.lower!="exit":
try:
b= int(a)
processing(b)
except ValueError:
print "Your input is not valid. Please enter a 'number'!"
time.sleep(1)
askNumber()
elif len(a)!=0 and a.lower=="exit":
answer()
else:
print "Your input can't be 'empty'"
time.sleep(1)
askNumber()
Run Code Online (Sandbox Code Playgroud)
因此,当在"a"的raw_input中输入"exit"时,应用的假设条件是elif,但最终应用if,最后打印"您的输入无效.请输入'数字'! " 对不起,如果它是明显的东西,我是一个乞丐,虽然我试图多次找到错误.
你需要调用该.lower()函数.
if len(a) != 0 and a.lower() != "exit":
# ...
elif len(a) != 0 and a.lower() == "exit":
Run Code Online (Sandbox Code Playgroud)
没有必要进行测试len(a)!=0,只需测试a自己:
if a and a.lower() != "exit":
# ...
elif a and a.lower() == "exit":
Run Code Online (Sandbox Code Playgroud)
空字符串False在布尔上下文中求值.