【问题标题】:Why does Exit code give a ValueError?为什么退出代码会给出 ValueError?
【发布时间】:2020-11-17 23:29:03
【问题描述】:

代码如下:

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif int(spam) > 2 and int(spam) > 1:
        print('Greetings!')
    elif str(spam) == 'exit':
        sys.exit()
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

错误如下:

exit
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 9, in <module>
ValueError: invalid literal for int() with base 10: 'exit'

[Program finished]

其他内容:

我尝试用谷歌搜索最后一个错误行,但它似乎与我的问题无关,与浮动有关。 我希望程序在我键入 exit 时退出,但我得到了那个错误。 所有其他事情似乎都在工作(1、2、3、-1)

另一件不起作用的事情是键入“退出”以外的内容。我收到相同的错误消息。 花了很多时间试图修复它无济于事。 请帮忙,谢谢。

【问题讨论】:

  • elif int(spam) &gt; 2 and int(spam) &gt; 1: 将尝试将spam 转换为intspam 等于'exit',因此不能转换为整数
  • 另外,如果int(spam) &gt; 2那么肯定int(spam) &gt; 1,所以条件int(spam) &gt; 2 and int(spam) &gt; 1可以简化为int(spam) &gt; 2
  • Pydroid 使这变得更加困难。通常,回溯会显示错误发生的行。
  • BTW,input()返回的是字符串,str(input())中不需要转换,str(spam) == 'exit'中绝对不需要再转换。
  • 哈哈,我真是个白痴,谢谢大家。

标签: python valueerror


【解决方案1】:

当您键入'exit' 时,将评估if 语句中第三个子句的条件。即int(spam) &gt; 2 and int(spam) &gt; 1。但是如果spam = 'exit',那么spam就不能转换成int,所以报错。

if 语句中重新排序子句是最简单的解决方案。

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif str(spam) == 'exit':
        sys.exit()
    elif int(spam) > 2 and int(spam) > 1:
        print('Greetings!')
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

现在,int(spam) &gt; 2 and int(spam) &gt; 1 仅在 str(spam) != 'exit' 适合您期望的(有限)输入时才被评估。

【讨论】:

  • 啊,我犯了教科书的错误哈。在 Automate The Boring Stuff 一书中阅读了有关重新排列错误的信息,并最终犯了同样的错误。不过这是一个很好的学习机会。感谢您抽出宝贵时间。
【解决方案2】:

您不能将整数与字符串进行比较。所以在比较之前检查字符串是否为整数:

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif spam.isdigit() and int(spam) > 1:
        print('Greetings!')
    elif str(spam) == 'exit':
        sys.exit()
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

如果输入的不是数字或“退出”,这将阻止它跌倒。

【讨论】:

  • spam.isdigit() 是一个很好的功能,谢谢。
【解决方案3】:

正如提到的 L.Grozinger,“退出比赛”的位置很重要。

您也可以添加ValueError 异常try/catch 来修复错误的用户输入...

import sys

print('Type something you idiot:')
while True:
    spam = str(input())
    print(spam)
    try:
        if spam == '1':
            print('Hello')
        elif spam == '2':
            print('Howdy')
        elif spam == "exit":
            sys.exit()
        elif int(spam) > 2 and int(spam) > 1:
            print('Greetings!')

    except ValueError:
        print('Type again you dumdum:')

【讨论】:

  • 感谢您抽出宝贵时间。那个'except'代码看起来很方便,刚刚了解它。
猜你喜欢
  • 2019-04-22
  • 2022-07-15
  • 1970-01-01
  • 2020-06-07
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
相关资源
最近更新 更多