mercredi 20 juillet 2016

Except ValueErrors

I'm a beginner at programming with python and confused on how exceptions work. I'm creating a menu list and dictionary for a restaurant and for the cost of item if user types in invalid number or a string I need it to except the value Error and ask the user again but still getting a value error code

while True:
    try:
        itemCost = float(raw_input("Please enter the cost of this item (ex: 22.45): "))
        if itemCost < 0: print "Invalid Number, please try again"
    except ValueError:
        print "Invalid Number, please try again"
    except NameError:
        print "Invalid Number, please try again"
    except SyntaxError:
        print "Invalid Number, please try again"

Aucun commentaire:

Enregistrer un commentaire