From 9fd54d6fbc852fcde0e3269f9c3d6c5432e0cd83 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Tue, 24 Jan 2023 02:37:00 +0200 Subject: [PATCH] Update Lesson 2 Task 4 to raise an error properly --- Lesson_2/task4.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Lesson_2/task4.py b/Lesson_2/task4.py index cb054a1..28be26e 100644 --- a/Lesson_2/task4.py +++ b/Lesson_2/task4.py @@ -2,19 +2,16 @@ # Author: Stanislav Mykhailenko # 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 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)) +except ValueError: print("Invalid number entered.")