From 538ddc2eeaafe0a7d6c94c03d36764ad091dd4b0 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Wed, 19 Oct 2022 20:51:38 +0300 Subject: [PATCH] Fix negative root calculation --- Lesson_1/task2.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Lesson_1/task2.py b/Lesson_1/task2.py index 094ffe3..ce14656 100644 --- a/Lesson_1/task2.py +++ b/Lesson_1/task2.py @@ -10,14 +10,12 @@ # 4 - Attempting to extract a root of a negative number with an even degree # 5 - Invalid operation entered -import sys - try: numberA = float(input("Enter number A: ")) numberB = float(input("Enter number B: ")) except ValueError: print("Invalid numbers entered.") - sys.exit(1) + exit(1) print( ''' @@ -41,20 +39,23 @@ elif operation == "*" or operation == "×": # ASCII asterisk or Unicode multipli elif operation == "/" or operation == "÷": # ASCII slash or Unicode division sign if numberB == 0: print("Division by zero.") - sys.exit(2) + exit(2) else: print(numberA / numberB) elif operation == "root" or operation == "√": # Unicode radical symbol if numberB < 1 or not numberB.is_integer(): print("Can't extract a root with non-natural degree.") - sys.exit(3) + exit(3) elif (numberB % 2) == 0 and numberA < 0: print("Can't extract a root of a negative number with an even degree.") - sys.exit(4) + exit(4) else: - print(numberA ** (1 / numberB)) + if numberA < 0: + print(-abs(numberA ** (1 / numberB))) + else: + print(numberA ** (1 / numberB)) elif operation == "square" or operation == "²": # Unicode superscript 2 print("Number A square: " + str(numberA ** 2) + "\nNumber B square: " + str(numberB ** 2)) else: print("Invalid operation.") - sys.exit(5) + exit(5)