Can´;t在python中将字符串转换为整数
我在尝试将字符串转换为整数时遇到此错误:Can´;t在python中将字符串转换为整数,python,python-3.x,Python,Python 3.x,我在尝试将字符串转换为整数时遇到此错误: Traceback (most recent call last): File "main.py", line 9, in <module>
Traceback (most recent call last):
File "main.py", line 9, in <module>
n = int(input())
ValueError: invalid literal for int() with base 10: 'python3 main.py'
您可能没有意识到解释器正在提示您输入。
input()
函数接受一个字符串参数,该参数将作为提示。这里的常见模式是:
n = None
while n is None:
try:
n = int(input('Please enter an integer: '))
except ValueError:
print('That was not an integer!')
它要求输入一个整数,您给了它文件名?看起来您输入了两次运行脚本的命令,而不是输入预期的数字。
n = None
while n is None:
try:
n = int(input('Please enter an integer: '))
except ValueError:
print('That was not an integer!')