From 838f6cd13123604599580d9a4500f71a7a3d7a1e Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Mon, 23 Jan 2023 23:35:41 +0200 Subject: [PATCH] Update Lesson 2 Task 3 to make it more simple in another way --- Lesson_2/task3.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Lesson_2/task3.py b/Lesson_2/task3.py index 75ef492..202bb19 100644 --- a/Lesson_2/task3.py +++ b/Lesson_2/task3.py @@ -2,21 +2,24 @@ # Author: Stanislav Mykhailenko # License: Unlicense -error = False +# Return codes: +# 0 - OK +# 1 - Invalid number entered + +import math, sys + +def numberError(): + print("Invalid number entered.") + sys.exit(1) try: number = int(input("Enter a natural number: ")) -except ValueError: error = True +except ValueError: numberError() -if not error and number < 1: error = True +if number < 1: numberError() -if not error: - while number > 0: - currentNumber = number - while currentNumber > 0: - print(currentNumber, end='') - if currentNumber > 1: print(' ', end='') - currentNumber = currentNumber - 1 - print('') - number = number - 1 -else: - print("Invalid number entered.") +for i in range(number, 0, -1): + currentNumber = i + for j in range(currentNumber, 0, -1): + print(j, end='') + if j > 1: print(' ', end='') + print('')