Update Lesson 2 Task 4 to work without libraries
This commit is contained in:
parent
35addea923
commit
6c4c9ac3c0
1 changed files with 10 additions and 15 deletions
|
@ -2,24 +2,19 @@
|
|||
# Author: Stanislav Mykhailenko
|
||||
# License: Unlicense
|
||||
|
||||
# Return codes:
|
||||
# 0 - OK
|
||||
# 1 - Invalid number entered
|
||||
|
||||
import sys
|
||||
|
||||
def numberError():
|
||||
print("Invalid number entered.")
|
||||
sys.exit(1)
|
||||
error = False
|
||||
|
||||
try: number = int(input("Enter a natural number: "))
|
||||
except ValueError: numberError()
|
||||
except ValueError: error = True
|
||||
|
||||
if number < 1: numberError()
|
||||
if not error and number < 1: error = True
|
||||
|
||||
if not error:
|
||||
factorial = 1
|
||||
|
||||
for i in range(2, number + 1):
|
||||
factorial = factorial * i
|
||||
else:
|
||||
print(str(number) + " factorial is " + str(factorial))
|
||||
else:
|
||||
print("Invalid number entered.")
|
Reference in a new issue