[英]Python 2.7.10 :Is there any other way to check whether an input is empty other than using len()
I am currently trying to make a maths quiz and I am working on trying to make the program not crash when the user doesn't answer a question(leaving the user input empty).This is what I have so far 我目前正在尝试进行数学测验,并且正在尝试使程序在用户不回答问题时不崩溃(将用户输入留空)。这就是我到目前为止的内容
useranswer=raw_input("what is 8-5?")
length=len(useranswer)
while length<=0:
useranswer=raw_input("please enter answer")
length=len(useranswer)
if int(useranswer)==3:
print "correct"
else:
print"sorry wrong"
right now I'm using the method of checking the length of the useranswer
using len(useranswer)
to see if there is anything in the input but this only works if i use raw_input
and not with a normal input
. 现在我正在使用通过
len(useranswer)
检查useranswer
的长度的方法,以查看输入中是否有任何内容,但这仅在我使用raw_input
而不是常规input
。
I wanted to know if there is any other easier way of checking if the user input is empty with a different method? 我想知道是否还有其他更简便的方法可以通过其他方法检查用户输入是否为空? I have looked for answers from a few other stackoverflow questions but none of them seemed suitable.
我从其他一些stackoverflow问题中寻找答案,但是似乎都不适合。
Thankyou! 谢谢!
You can use 您可以使用
if useranswer:
# do what you want to do here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.