简体   繁体   English

Python 2.7.10:除了使用len()以外,还有其他方法可以检查输入是否为空

[英]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.

相关问题 除了“ try…except”和“ .isdigit()”以外,还有其他方法可以检查Python 2中的用户输入吗? - Is there a way other than 'try…except' and '.isdigit()' to check user input in Python 2? 除了使用tkinter Python进行网格设计以外,还有其他设计方法吗? - Any other way to design other than grid with tkinter Python? python字典检查是否存在除给定键以外的任何键 - python dictionary check if any key other than given keys exist 如何在 python 变量或任何其他方式中传递空值 - How to pass empty value in python variable or any other way to do 使用Python 3上的任何数据结构检查组是否是其他组的子组的最有效方法 - Most efficient way to check if a group is a subgroup of other group using any data structure on Python 3 有没有其他方法可以使用多个 arguments 写入文件? - Is there any other way to write into a file using more than one arguments? 检查一个字符串是否有除'*'之外的任何其他字符 - check if a string has any other character than '*' 检查文件是否具有标题以外的内容 - Check whether file has content other than header Python:使用 len() 根据其他列的值创建新列 - Python: Create New Column based on values of other column using len() 检查字符串是否为英语以外的其他语言的字母 - Check whether a string is alphabetical for languages other than english
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM