-1

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);
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Jane
  • 5
  • 4
  • 2
    In python you don't need `;` and you can only convert strings to int with only numbers in it. – Ch3steR Feb 15 '20 at 15:32
  • Welcome to Stack Overflow! Check out the [tour] and [ask]. What's your question? Please [edit] to clarify. If you want debugging help, you need to provide a [mre], but atm you're missing the input. – wjandrea Feb 15 '20 at 15:34
  • 1
    related, see [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). And also read about the [`int()`](https://docs.python.org/3/library/functions.html#int) constructor – Tomerikoo Feb 15 '20 at 15:40
  • It works fine for me as long as I introduce numbers. If I introduce characters it throws the error you say, which is normal. Just remove `;` you don't need it in python – Capie Feb 15 '20 at 15:45

1 Answers1

0

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.

Amit
  • 2,018
  • 1
  • 8
  • 12