I've problem with, after if-statement limitation, code should follow user input again to take valid number. I just learned that using of try, except ValueError usage and couldn't fit it correctly i guess.
def factorial(num):
fact=1
for i in range(1,num+1):
fact*=i
print(num,"! =",fact)
while True:
try:
user_input=int(input("Give me a number:"))
if user_input <= 0:
print("Number should be greater than zero!")
except ValueError:
print("Invalid Character!")
else:
factorial(user_input)
-Actual result-
Give me a number:0
Number should be greater than zero!
0 ! = 1
0 ! = 1 calculation of fuctorial shouldn't be there. How can I fix this?
Aucun commentaire:
Enregistrer un commentaire