简体   繁体   中英

python invalid literal error

so I have this code

data = input()
m = data.split(',')

for i in range(0, len(m)):

    print(int(m[i]) )

but then when i run it and type in "1,2,3", i get this error:

    print(int(m[i]))
ValueError: invalid literal for int() with base 10: '"1'

what did i do wrong?

using python 3

As you have your code, the input you type in should be numbers separated by commas. You do not have to include the quotes, just type 1,2,3 and nothing else. You only need the quotes only if you are writing actual code writing string literals. In this case, you are just taking input and it will already be strings.

The problem is that you are typing literally "1,2,3" and it tries to parse the first int the string, "1 which is invalid.

看来你输入了文字引号( " )作为输入的一部分; int()不知道如何解析它们。你需要先将它们剥离出来:

data = input().strip('"')

看起来你的输入中有一个额外的"字符int()只会转换由所选基数中的数字组成的字符串(默认基数为10,所以它接受[0-9])。如果字符串输入中出现任何其他字符,它引发了这个错误。

Your input to input :

"1,2,3"

is appropriate for the Python 2 input . Python 3 input is like Python 2 raw_input and all its input is already considered a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM