2022-10-18 14:02:29 +00:00
|
|
|
|
# Lesson 1 Task 2: calculator
|
|
|
|
|
# Author: Stanislav Mykhailenko
|
|
|
|
|
# License: Unlicense
|
|
|
|
|
|
|
|
|
|
# Return codes:
|
|
|
|
|
# 0 - OK
|
|
|
|
|
# 1 - Invalid numbers entered
|
|
|
|
|
# 2 - Division by zero
|
2022-10-19 17:06:42 +00:00
|
|
|
|
# 3 - Attempting to use a root with non-natural degree
|
|
|
|
|
# 4 - Attempting to extract a root of a negative number with an even degree
|
|
|
|
|
# 5 - Invalid operation entered
|
2022-10-18 14:02:29 +00:00
|
|
|
|
|
2022-10-19 17:54:24 +00:00
|
|
|
|
import sys
|
|
|
|
|
|
2022-10-18 14:02:29 +00:00
|
|
|
|
try:
|
|
|
|
|
numberA = float(input("Enter number A: "))
|
|
|
|
|
numberB = float(input("Enter number B: "))
|
|
|
|
|
except ValueError:
|
|
|
|
|
print("Invalid numbers entered.")
|
2022-10-19 17:54:24 +00:00
|
|
|
|
sys.exit(1)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
|
2022-10-19 17:06:42 +00:00
|
|
|
|
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"
|
|
|
|
|
)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
|
2022-10-19 17:06:42 +00:00
|
|
|
|
operation = input("Enter operation: ")
|
2022-10-18 14:02:29 +00:00
|
|
|
|
|
|
|
|
|
if operation == "+":
|
2022-10-19 17:06:42 +00:00
|
|
|
|
print(numberA + numberB)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
elif operation == "-" or operation == "−": # ASCII hyphen-minus or Unicode minus
|
2022-10-19 17:06:42 +00:00
|
|
|
|
print(numberA - numberB)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
elif operation == "*" or operation == "×": # ASCII asterisk or Unicode multiplication sign
|
2022-10-19 17:06:42 +00:00
|
|
|
|
print(numberA * numberB)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
elif operation == "/" or operation == "÷": # ASCII slash or Unicode division sign
|
|
|
|
|
if numberB == 0:
|
|
|
|
|
print("Division by zero.")
|
2022-10-19 17:54:24 +00:00
|
|
|
|
sys.exit(2)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
else:
|
2022-10-19 17:06:42 +00:00
|
|
|
|
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.")
|
2022-10-19 17:54:24 +00:00
|
|
|
|
sys.exit(3)
|
2022-10-19 17:06:42 +00:00
|
|
|
|
elif (numberB % 2) == 0 and numberA < 0:
|
|
|
|
|
print("Can't extract a root of a negative number with an even degree.")
|
2022-10-19 17:54:24 +00:00
|
|
|
|
sys.exit(4)
|
2022-10-18 14:02:29 +00:00
|
|
|
|
else:
|
2022-10-19 17:51:38 +00:00
|
|
|
|
if numberA < 0:
|
|
|
|
|
print(-abs(numberA ** (1 / numberB)))
|
|
|
|
|
else:
|
|
|
|
|
print(numberA ** (1 / numberB))
|
2022-10-19 17:06:42 +00:00
|
|
|
|
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.")
|
2022-10-19 17:54:24 +00:00
|
|
|
|
sys.exit(5)
|