Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can´;t在python中将字符串转换为整数_Python_Python 3.x - Fatal编程技术网

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!')