Python Forum
why does integer input give error invalid literal for int()
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why does integer input give error invalid literal for int()
#1
I am keep getting an error if I put in something other than an integer. I want the loop to reiterate if say enter is pushed or if a letter is input. I want to give the user the error message I put in the else statement and return back to asking for the input again. Thank you.  

pizza_high = 0
      
      while not repeat:
        pizza_high = int(input('How many pizzas would you like to order:'))
        if pizza_high != int:
          repeat = True
        else:
          print('**ERROR** Please use whole numbers only!!')
          pizza_high = 0
      for x in range(0, pizza_high): 
        print('pizzas')
This is the input if "5" was selected, it works properly.

How many pizzas would you like to order: 5
    pizzas
    pizzas
    pizzas
    pizzas
    pizzas
    

However if I hit enter or put in a letter it gives this error

   
How many pizzas would you like to order: t
    ValueError: invalid literal for int() with base 10: ';'
Reply
#2
Use a try/except instead of in if block, since int() throws errors if you try to convert something that can't be converted:
>>> int("43")
43
>>> int("four")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'four'

>>> try:
...   num = int("43")
... except ValueError as err:
...   print("That's not a number...")
...   print(err)
...
>>> num
43
>>> try:
...   num = int("four")
... except ValueError as err:
...   print("That's not a number...")
...   print(err)
...
That's not a number...
invalid literal for int() with base 10: 'four'
Reply
#3
You should go and read about exception handling, as that's often used to signal errors in Python. We say that int() raises an exception of type ValueError when its argument can't be converted to an integer. So, as shown above, the solution is to handle or catch the ValueError with try and except.

Line 5 in your code is also wrong - you're trying to compare the value of pizzas with the function int(), which makes no sense.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error: invalid command 'egg_info' TimTu 0 911 Jul-27-2023, 07:30 AM
Last Post: TimTu
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,663 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Invalid format specifier Error Led_Zeppelin 2 7,601 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Error in Using INPUT statement gunwaba 1 2,017 Jul-03-2022, 10:22 PM
Last Post: deanhystad
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,932 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,095 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,177 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  Invalid syntax error - need help fixing calgk01 3 3,228 Feb-23-2021, 08:41 PM
Last Post: nilamo
  ValueError: invalid literal for int() with base 10: omega_elite 5 5,679 Dec-30-2020, 06:11 AM
Last Post: delonbest
  Error on nested loop : Invalid syntax dvazquezgu 3 3,178 Nov-25-2020, 10:04 AM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020