From 6c4c9ac3c064327cf329509c8f47ce73656fc717 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Wed, 4 Jan 2023 01:03:02 +0200 Subject: [PATCH] Update Lesson 2 Task 4 to work without libraries --- Lesson_2/task4.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Lesson_2/task4.py b/Lesson_2/task4.py index e7cc733..59f0cb9 100644 --- a/Lesson_2/task4.py +++ b/Lesson_2/task4.py @@ -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 -factorial = 1 +if not error: + factorial = 1 -for i in range(2, number + 1): - factorial = factorial * i + for i in range(2, number + 1): + factorial = factorial * i + else: + print(str(number) + " factorial is " + str(factorial)) else: - print(str(number) + " factorial is " + str(factorial)) + print("Invalid number entered.") \ No newline at end of file