[Solved] Try / Except Statements in python [closed]


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

1

solved Try / Except Statements in python [closed]