-5

I'm trying to create a python program that uses try/except statements. Everything works fine except, I'm still getting a value error, even after defining the except statement.

my code:

    inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283: ['OLIVE OIL', 10.99, 100], 839529: ['TOMATOS 1LB', 1.29, 25], 
             483946: ['MILK 1/2G', 3.45, 35], 493402: ['FLOUR 5LB', 2.99, 40], 485034: ['BELL PEPPERS 1LB', 1.35, 28], 
             828391: ['WHITE TUNA', 1.69, 100], 449023: ['CHEESE 1/2LB', 4.99, 15]}

upc_input = int(input('Enter a UPC number:  '))
description = input('Enter a, item description: ')
unit_price = float(input('Enter the unit price: '))
quantity = int(input('Enter th quantity: '))

try:
    if upc_input in inventory.keys():
        print('Inventory Updating')
        inventory[upc_input] = [description,unit_price,quantity]

except Excetption as exp:
    print('Error occurred!',exp)

try:
    if upc_input not in inventory.keys():
        print('Adding new inventory')
        inventory[upc_input] = [description,unit_price,quantity]

except Excetption as exp:
    print('Error occurred!',exp)


print()    
print(inventory)



---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-1313cc5c500f> in <module>()
----> 1 upc_input = int(input('Enter a UPC number:  '))
      2 description = input('Enter a, item description: ')
      3 unit_price = float(input('Enter the unit price: '))
      4 quantity = int(input('Enter th quantity: '))
      5 

ValueError: invalid literal for int() with base 10: 'eeeee'
KKH
  • 7
  • 6
  • Error on which line? – user202729 Aug 18 '18 at 15:17
  • 2
    Suspect typo error → off-topic. `Exception` not `Excetption`. – user202729 Aug 18 '18 at 15:18
  • 1
    I suspect you are not putting `try: ... except: ...` around the right code and are likely getting a `ValueError` from you `int()` or `float()` calls because I can't see how the code you have wrapped in `try, except` would `raise` an exception. – AChampion Aug 18 '18 at 15:21
  • The typo would raise a `NameError` only if it gets to that line of code. – AChampion Aug 18 '18 at 15:22
  • When asking about code that produces an Exception you should always include the complete Traceback in the question. Copy the Traceback and paste it in the question, then format it as code (select it and type ctrl-k). The Traceback tells you where the error occurred. – wwii Aug 18 '18 at 15:22
  • Great input from both. Thanks I'll correct the typo. – KKH Aug 18 '18 at 15:23
  • @ wwii, sorry I'll edit the post. – KKH Aug 18 '18 at 15:24
  • The exception is probably from the `int(input())` or similar lines. Ironically, they're the ones I'd put a try except around since your if statements should ensure there are no exceptions – N Chauhan Aug 18 '18 at 15:25
  • Thanks N Chauhan. – KKH Aug 18 '18 at 15:26
  • 1
    Your edit didn't fix either of the two typos with `Excetption` – roganjosh Aug 18 '18 at 15:28
  • Thank you all for your insight. I was able to fix the issue simply by moving all the input statements into (local) the try statement. – KKH Aug 18 '18 at 15:33

1 Answers1

0
inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283: ['OLIVE OIL', 10.99, 100], 839529: ['TOMATOS 1LB', 1.29, 25], 
             483946: ['MILK 1/2G', 3.45, 35], 493402: ['FLOUR 5LB', 2.99, 40], 485034: ['BELL PEPPERS 1LB', 1.35, 28], 
             828391: ['WHITE TUNA', 1.69, 100], 449023: ['CHEESE 1/2LB', 4.99, 15]}

while True:
     try:
          upc_input = int(input('Enter a UPC number:  '))
          break
     except Exception:
         print("Oops!  That was no valid number.  Try again...")

if upc_input in inventory.keys():
    description = input('Enter a, item description: ')
    unit_price = float(input('Enter the unit price: '))
    quantity = int(input('Enter th quantity: '))
    print('Inventory Updating')
    inventory[upc_input] = [description,unit_price,quantity]

if upc_input not in inventory.keys():
    description = input('Enter a, item description: ')
    unit_price = float(input('Enter the unit price: '))
    quantity = int(input('Enter th quantity: '))
    print('Adding new inventory')
    inventory[upc_input] = [description,unit_price,quantity]

print()    
print(inventory)

Here! I checked your code and found the following, a) you mispelled "Exception" and b) there is a logic mistake. You ask first for the number and then use the statement try. You MUST have the input INSIDE the try statement, otherwise your program is trying nothing

Check in my code I have the input statement inside the try:, and if everything goes right, then it will move on to asking for the other inputs and setting them up depending wether the reference number is/isnt in the list

JDAR
  • 36
  • 5