【问题标题】:How do you get Python to detect for no input如何让 Python 检测无输入
【发布时间】:2014-12-01 07:38:22
【问题描述】:

我是 python 编码的新手,我正在填写一些需要大量输入的代码。它要求的一件事是,如果用户按下回车键并且没有输入任何输入,程序执行一个动作。我的问题是如何让 python 来检查它。会不会:

if input == "":
    #action

还是别的什么?谢谢你的帮助。

编辑:这是我的代码目前的样子以供参考。

 try:
     coinN= int(input("Enter next coin: "))

     if coinN == "" and totalcoin == rand: 
         print("Congratulations. Your calculations were a success.")
     if coinN == "" and totalcoin < rand:
         print("I'm sorry. You only entered",totalcoin,"cents.")  

 except ValueError:
     print("Invalid Input")
 else:
     totalcoin = totalcoin + coinN

【问题讨论】:

  • 很确定你需要 2 个==s 并将" " 更改为""
  • 对。谢谢。忘记了。
  • coinN 是一个整数。您正在检查它是否为空字符串

标签: python input enter


【解决方案1】:

我知道这个问题已经过时了,但我仍在分享您的问题的解决方案,因为它可能对其他人有所帮助。要检测 Python 中没有输入,您实际上需要检测“文件结束”错误。这是没有输入时造成的:
这可以通过以下代码检查:

final=[]
while True:
    try:
         final.append(input())   #Each input given by the user gets appended to the list "final"
    except EOFError:
         break                   #When no input is given by the user, control moves to this section as "EOFError or End Of File Error is detected"

希望这会有所帮助。

【讨论】:

  • 太好了,这救了我!
【解决方案2】:

实际上是一个空字符串

""

而不是

" "

后者是空格符

编辑
其他一些注意事项

  1. 不要使用 input 作为 Python 关键字的变量名

  2. 比较相等使用==而不是=,后者是赋值运算符,它试图修改左边的值。

【讨论】:

  • 对。我试过了。但是,它没有转到我的响应命令,而是转到了我的异常命令并被识别为 ValueError。
  • @user3495234:您是否犯了一个经典的错误,即尝试在之前检查您是否可以做某事,而不是之后?例如,在您的if 语句之前调用int
  • 我在上面添加了它的样子。 int 之后应该消失吗?
【解决方案3】:

只是另一个提示:

在 python 中,您不需要对空字符串进行相等性测试。请改用truth value testing。这更像是pythonic。

if not coinN:

真值测试涵盖以下测试:

  • 错误
  • 任何数字类型的零,例如,0、0L、0.0、0j。
  • 任何空序列,例如,''、()、[]。
  • 任何空映射,例如 {}。
  • 用户定义类的实例,如果该类定义了 nonzero() 或 len() 方法,则该方法返回整数零或布尔值 False。 1

例子:

>>> s = ''
>>> if not s:
...     print 's is empty'
...
s is empty
>>>

【讨论】:

  • 如果您想要一个可以使用 0 但没有输入就不行的条件怎么办。这似乎是 OP 的问题。
【解决方案4】:

我是 python 新手,正在寻找解决类似问题的方法。我知道这是一个非常古老的帖子,但我想我会尝试一下。如果我正确理解了您的问题以及您要达到的目标,那么这对我来说很好。 (只要您不尝试输入字母!) 之前发过,但是发错了,不好意思。

totalcoins = None
coinN = None
sum_total = range

while coinN != '0' and totalcoins != '0':
    coinN = input("Please enter first amount:   ")
    if coinN == "":
        print("You didn't enter anything")
    else:
        totalcoins = input("Please enter second amount   ")
        if totalcoins == "":
            print("You didn't enter anything")
        else:
            sum_total = int(coinN) + int(totalcoins)
            if sum_total in range(100):
                    print('Sorry, you only entered {} cents'.format(sum_total))
            else:
                if sum_total == 100:
                    print('The sum of {0} and {1} is = 1 rand     '.format(coinN, totalcoins, sum_total))
            if sum_total >=100:
                print('The sum of {0} and {1} is = {2} rand   '.format(coinN, totalcoins, sum_total))
                print("\n")

【讨论】:

    【解决方案5】:

    编辑:

    这样的事情怎么样:

     try:
         coinN = input("Enter next coin: ")
         if coinN.isdigit(): # checks whether coinN is a number
             if coinN == "" and totalcoin == rand:
                 print("Congratulations. Your calculations were a success.")
             if coinN == "" and totalcoin < rand:
                 print("I'm sorry. You only entered",totalcoin,"cents.")
         else:
             raise ValueError
    
     except ValueError:
         print("Invalid Input")
     else:
         totalcoin = totalcoin + int(coinN) # convert coinN to int for addition
    

    【讨论】:

    • 我试过了,但不幸的是,不断打印出来的消息是“无效输入”
    • 啊,我明白了,你正在测试一个带有 "" 的字符串,但你输入的是一个 int。我会更新我的帖子。
    • 不幸的是,我得到了同样的结果。
    • 你确定这与totalcoin或rand无关吗?
    • 我不这么认为,因为当我取出 totalcoin 部分时我仍然得到无效输入,并且只留下它好像不是 coinN:
    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 2015-01-04
    • 2015-05-03
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多