Update Lesson 2 Task 4 to work without libraries

This commit is contained in:
Stanislav Mykhailenko 2023-01-04 01:03:02 +02:00
parent 35addea923
commit 6c4c9ac3c0
GPG key ID: 1E95E66A9C9D6A36

View file

@ -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.")