0

I'm new in Python 3 and need some help, my error is:

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

any ideas? The code I'm using is the following:

liste = [1, 2, 3]

def liste_pop():
    print(liste)
    pop = int(input('remove = Enter um das letzte Element der Liste auszugeben + entfernen oder die Position eingeben'))
    liste.pop(pop)
    return
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Cafekev
  • 11
  • 1
  • 5
  • 2
    You did not enter a number during `input`, instead you simply pressed enter (and got back the empty string). Add code to handle the situation where a user enters invalid input (`try-except` in a loop). – Dimitris Fasarakis Hilliard Dec 17 '16 at 20:19
  • 1
    Possible duplicate of [ValueError: invalid literal for int() with base 10: 'stop'](http://stackoverflow.com/questions/16742432/valueerror-invalid-literal-for-int-with-base-10-stop) – MalloyDelacroix Dec 17 '16 at 20:28
  • not sure this would count as a dup of that @MalloyDelacroix but the solution posted there definitely works for this question. – Tadhg McDonald-Jensen Dec 17 '16 at 20:30
  • I'd agree with Tadhg, that's why I posted an answer (didn't find a good dupe target after posting the comment). Either way, I'll keep on looking for a bit and see if I can find anything better. – Dimitris Fasarakis Hilliard Dec 17 '16 at 20:34

1 Answers1

2

You did not enter a number during input, instead you simply pressed enter (and got back the empty string). You need to add code to handle the situation where a user enters invalid input, in Python, this is performed with a try-except where you specify the error you're expecting ValueError in the except clause:

def liste_pop():
    print(liste)
    while True:
        try:
            pop = int(input('remove = Enter um das letzte Element der Liste auszugeben + entfernen oder die Position eingeben'))
            break
        except ValueError as e:
            print("Only numbers accepted")
    liste.pop(pop)

Of course, this has an additional issue, what if a user enters a number outside the accepted range? An IndexError is raised; you'll need another try-except for that (I'll let you handle it on your own.)

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253