mercredi 15 juin 2016

Incorrect Looping and returning of variable


My code below is checking if the input (quantity) is a number. If it is a number first time, it returns the number fine. However, if you were to put a letter in, and then when the function loops enter a number, '0' is returned rather than the number you inputted.

def quantityFunction():
    valid = False
    while True:
            quantity = input("Please enter the amount of this item you would like to purchase: ")
            for i in quantity:
                try:
                    int(i)
                    return int(quantity)
                except ValueError:
                    print("We didn't recognise that number. Please try again.")
                    quantityFunction()
                    return False

Am I looping the function incorrectly?


Aucun commentaire:

Enregistrer un commentaire