Decision
There are better ways to do this, but given the structure of the code, you can use else . See documents .
num = input("input your number to be factorialised here!: ") try: num1 = int(num) except ValueError: print("That not a number!") else: if num1 < 0: print("You can't factorialise a negative number") elif num1 == 0: print("the factorial of 0 is 1") else: ans = 1 for i in range(1,num1+1): ans = ans * i print("the factorial of", num, "is", ans)
The else clause is executed only if there are no exceptions.
suggestions
In order not to give away answers to your homework, here are some tips you should learn to clear your code:
- Can you use range more efficiently? Hint: You can iterate over decreasing numbers by setting
step to a negative integer. - Can you find a way to get rid of the
num1 == 0 check?
source share