vendredi 23 juillet 2021

Can't make an IF/ELSE or at least a loop to work to make sure the input is a positive integer before doing a calculation [duplicate]

What I have so far that works just fine with a positive integer input. The purpose is to calculate the square of each number between 0 and the input.

x = int(input("Please enter a number: "))

for x in range(1, x+1):
        print(x ** 2)

This is including a if/else to make sure the input is a positive integer but it ends if it is negative instead of going back to asking again. Appreciate any help.

x = int(input("Please enter a number: "))

if x < 0:
    print("That is not a positive number, try again.")

else:
    for x in range(1, x+1):
        print(x ** 2)

Aucun commentaire:

Enregistrer un commentaire