Make Lesson 1 Task 2 use only one operation, added support for any nth roots
This commit is contained in:
parent
8a65326755
commit
434c7bec09
1 changed files with 31 additions and 25 deletions
|
@ -6,10 +6,11 @@
|
||||||
# 0 - OK
|
# 0 - OK
|
||||||
# 1 - Invalid numbers entered
|
# 1 - Invalid numbers entered
|
||||||
# 2 - Division by zero
|
# 2 - Division by zero
|
||||||
# 3 - Invalid first operation entered
|
# 3 - Attempting to use a root with non-natural degree
|
||||||
# 4 - Square root of a negative number calculation
|
# 4 - Attempting to extract a root of a negative number with an even degree
|
||||||
|
# 5 - Invalid operation entered
|
||||||
|
|
||||||
import sys, math
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
numberA = float(input("Enter number A: "))
|
numberA = float(input("Enter number A: "))
|
||||||
|
@ -18,37 +19,42 @@ except ValueError:
|
||||||
print("Invalid numbers entered.")
|
print("Invalid numbers entered.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
operation = input("Enter operation (+, − or -, × or *, ÷ or /): ")
|
print(
|
||||||
|
'''
|
||||||
|
Valid operations:
|
||||||
|
Addition: +
|
||||||
|
Subtraction: - or −
|
||||||
|
Multiplication: * or ×
|
||||||
|
Division: / or ÷
|
||||||
|
Root: root or √ (extracted from the first number, with the second number being its degree)
|
||||||
|
Square: square or ² (of both numbers)''', end="\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
# The other part of the task is not clearly defined. It asks to implement square root of a number, or its square, but it can only be done with one number. Assuming it is the result of the first operation and saving it…
|
operation = input("Enter operation: ")
|
||||||
result: float
|
|
||||||
|
|
||||||
if operation == "+":
|
if operation == "+":
|
||||||
result = numberA + numberB
|
print(numberA + numberB)
|
||||||
elif operation == "-" or operation == "−": # ASCII hyphen-minus or Unicode minus
|
elif operation == "-" or operation == "−": # ASCII hyphen-minus or Unicode minus
|
||||||
result = numberA - numberB
|
print(numberA - numberB)
|
||||||
elif operation == "*" or operation == "×": # ASCII asterisk or Unicode multiplication sign
|
elif operation == "*" or operation == "×": # ASCII asterisk or Unicode multiplication sign
|
||||||
result = numberA * numberB
|
print(numberA * numberB)
|
||||||
elif operation == "/" or operation == "÷": # ASCII slash or Unicode division sign
|
elif operation == "/" or operation == "÷": # ASCII slash or Unicode division sign
|
||||||
if numberB == 0:
|
if numberB == 0:
|
||||||
print("Division by zero.")
|
print("Division by zero.")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
else:
|
else:
|
||||||
result = numberA / numberB
|
print(numberA / numberB)
|
||||||
else:
|
elif operation == "root" or operation == "√": # Unicode radical symbol
|
||||||
print("Invalid operation.")
|
if numberB < 1 or not numberB.is_integer():
|
||||||
sys.exit(3)
|
print("Can't extract a root with non-natural degree.")
|
||||||
|
sys.exit(3)
|
||||||
print("Result: " + str(result))
|
elif (numberB % 2) == 0 and numberA < 0:
|
||||||
|
print("Can't extract a root of a negative number with an even degree.")
|
||||||
# …and asking to do sqrt or square with it.
|
|
||||||
secondOperation = input("Do anything else with the result (√ or sqrt, ² or square, any other input will be ignored)? ")
|
|
||||||
|
|
||||||
if secondOperation == "√" or secondOperation == "sqrt": # Unicode radical symbol
|
|
||||||
if result < 0:
|
|
||||||
print("Can't get a square root of a negative number.")
|
|
||||||
sys.exit(4)
|
sys.exit(4)
|
||||||
else:
|
else:
|
||||||
print(math.sqrt(result))
|
print(numberA ** (1 / numberB))
|
||||||
elif secondOperation == "²" or secondOperation == "square": # Unicode superscript 2
|
elif operation == "square" or operation == "²": # Unicode superscript 2
|
||||||
print(math.pow(result, 2))
|
print("Number A square: " + str(numberA ** 2) + "\nNumber B square: " + str(numberB ** 2))
|
||||||
|
else:
|
||||||
|
print("Invalid operation.")
|
||||||
|
sys.exit(5)
|
||||||
|
|
Reference in a new issue