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
|
# Author: Stanislav Mykhailenko
|
||||||
# License: Unlicense
|
# License: Unlicense
|
||||||
|
|
||||||
# Return codes:
|
error = False
|
||||||
# 0 - OK
|
|
||||||
# 1 - Invalid number entered
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
def numberError():
|
|
||||||
print("Invalid number entered.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
try: number = int(input("Enter a natural number: "))
|
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
|
||||||
|
|
||||||
factorial = 1
|
if not error:
|
||||||
|
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))
|
||||||
else:
|
else:
|
||||||
print(str(number) + " factorial is " + str(factorial))
|
print("Invalid number entered.")
|
Reference in a new issue