jeudi 3 septembre 2020

Using nested if statements within a while loop (python)

Here's my code:

#Greeting
print('\n---Sales Discount Calculator---\n')

#User Input
packageCount = input('Packages Purchased: ')

#To check whether packageCount is an integer or not
#loop continues until a positive integer is inputted
validInput = False

while (validInput == False):
    if (packageCount.isdigit()):
        packageCount = int(packageCount)
    if (packageCount != 0):
        validInput = True
        print(packageCount)
    else:
        print('Invalid Input. Try again...')
        packageCount = input('Packages Purchased: ')

I'm trying to see if the input from the user is a positive integer and also not a zero. I need to check if it's an integer first(the first if-statement) because you can't compare type string to numbers. If it is an integer, then I need to check if it is a zero or not(second if-statement). When the second if-statement is not present, the code checks for both a positive integer and if it's a string or not, but somehow when I include the second if statement and type in a string, it continues without printing the 'Invalid Input'(the else). *This causes issues later on.

Aucun commentaire:

Enregistrer un commentaire