简体   繁体   中英

ValueError: ValueError: invalid literal for int() with base 10 in Python

It says ValueError. I've tried using int('') as well. Didn't work.

first = []
a=int(input());

for i in range(0,a):
    ele = int(input());
    first.append(ele);

second = first[::-1];

th = [x + y for x, y in zip(first, second)];
print(th);

According to the official documentation , the class constructor for int is as follows.

class int(x, base=10)

So a base of 10 is assumed (I guess because a decimal number system is the most prevalent) unless and otherwise you state something else.

So what should be x?

Again as per the docs.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base.

So the x should be an integer literal of that base. If you supply an alphabet say 'A' for int like below, the 'A' can not be represented as an integer using a decimal number system. Trying to do the below ..

Num = int('A')
print(Num)

will get you an error ...

ValueError: invalid literal for int() with base 10: 'A'

However, if your number system (base) is hexadecimal then you can say base=16 and it will faithfully convert 'A' it into an integer. So the below ...

Num = int('A', base=16)
print(Num)

gives ..

10

So please check what is your number system (base) is and see if the string (that you are trying to convert to an integer) makes sense for that particular number system.

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