Update Lesson 2 Task 4 to raise an error properly
This commit is contained in:
parent
838f6cd131
commit
9fd54d6fbc
1 changed files with 7 additions and 10 deletions
|
@ -2,19 +2,16 @@
|
||||||
# Author: Stanislav Mykhailenko
|
# Author: Stanislav Mykhailenko
|
||||||
# License: Unlicense
|
# License: Unlicense
|
||||||
|
|
||||||
error = False
|
try:
|
||||||
|
number = int(input("Enter a natural number: "))
|
||||||
|
if number < 1:
|
||||||
|
raise ValueError("A natural number is required.")
|
||||||
|
|
||||||
try: number = int(input("Enter a natural number: "))
|
|
||||||
except ValueError: error = True
|
|
||||||
|
|
||||||
if not error and number < 1: error = True
|
|
||||||
|
|
||||||
if not error:
|
|
||||||
factorial = 1
|
factorial = 1
|
||||||
|
|
||||||
for i in range(2, number + 1):
|
for i in range(2, number + 1):
|
||||||
factorial = factorial * i
|
factorial = factorial * i
|
||||||
else:
|
|
||||||
print(str(number) + " factorial is " + str(factorial))
|
print(str(number) + " factorial is " + str(factorial))
|
||||||
else:
|
except ValueError:
|
||||||
print("Invalid number entered.")
|
print("Invalid number entered.")
|
||||||
|
|
Reference in a new issue