【发布时间】: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) > 2 and int(spam) > 1:将尝试将spam转换为int。spam等于'exit',因此不能转换为整数 -
另外,如果
int(spam) > 2那么肯定int(spam) > 1,所以条件int(spam) > 2 and int(spam) > 1可以简化为int(spam) > 2 -
Pydroid 使这变得更加困难。通常,回溯会显示错误发生的行。
-
BTW,
input()返回的是字符串,str(input())中不需要转换,str(spam) == 'exit'中绝对不需要再转换。 -
哈哈,我真是个白痴,谢谢大家。
标签: python valueerror