-5

Why do I get this error?

and its output:

lst.append(int(input()))
ValueError: invalid literal for int() with base 10: ''
lst = []
for n in range(n):
    lst.append(int(input()))
AsukaMinato
  • 1,017
  • 12
  • 21
  • https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10 Is it what you are looking for? – toptecshare Jun 03 '22 at 12:02
  • 1
    What do you enter? Please, post [mre], incl. full traceback you get. It looks you just hit Enter – buran Jun 03 '22 at 12:02
  • error means you try to convert empty string - `int("")` - maybe you should first get `input()`, next check if it not empty, and next convert to `int` and put on list. But you may have similar problem when you input text i.e. `Hello` instead of string with number. And maybe it would be to use `try/except` to catch error when it tries to convert `int("")` or `int("Hello")` – furas Jun 03 '22 at 13:55

2 Answers2

2

The error is given because you are pressing enter, sending an empty string '' to the input(), which cannot be interpreted as an int.

Also its probably a good idea to change first n in for n in range(n) with something else.

Ach113
  • 1,775
  • 3
  • 18
  • 40
  • @Kelly edited. At first I thought it would cause incorrect number of iterations, which may have caused the invalid input. Then I realized `range(n)` gets calculated once and after that `n` variable can freely be used within the loop if needed. – Ach113 Jun 03 '22 at 12:55
-1

Probably because you're inputting something that's not an int.

Your int(input()) tries to convert its type to int, so if you're typing 'hello' or 5.154 it will fail.

You can just leave lst.append(input()) to get any value as string or lst.append(float(input())) if you're inputting a float number.

AsukaMinato
  • 1,017
  • 12
  • 21
Burgos
  • 71
  • 8