Untitled
unknown
python
3 years ago
2.1 kB
7
Indexable
from calc_logo import logo
# Calculator
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
try:
return n1 / n2
except:
print("You cannot divide by zero!")
return my_calculator()
def exponentiation(n1, n2):
return n1 ** n2
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
"**": exponentiation
}
def get_first_input():
num1 = input("What's the first number?: ")
try:
return float(num1)
except:
print("Invalid data, given number must be an integer or a float!")
return get_first_input()
def get_second_input():
num2 = input("What's the next number?: ")
try:
return float(num2)
except:
print("Invalid data, given number must be an integer or a float!")
return get_second_input()
def my_calculator():
print("Welcome to Marta's calculator!")
print(logo)
num1 = get_first_input()
for symbol in operations:
print(symbol)
flag = True
while flag:
available_operations = ["+", "-", "*", "/", "**"]
operation_symbol = input("Pick an operation symbol: ")
if operation_symbol in available_operations:
num2 = get_second_input()
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
users_decision = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calc: ")
available_decision = ['y', 'n']
if users_decision == 'y':
num1 = answer
elif users_decision == 'n':
flag = False
my_calculator()
elif users_decision not in available_decision:
raise ValueError("Please choose 'y' for 'yes' or 'n' for 'no'!")
else:
print("Please choose the correct operation symbol.")
my_calculator()
my_calculator()Editor is loading...