A Python program implementing a basic calculator with the ability to perform addition, subtraction, multiplication, division, and exponentiation. The program prompts users to input two numbers and choose an operation. It then displays the result and allows users to continue calculations or start a new one.
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Exponentiation (**)
- Enter the first number.
- Choose an operation from the available options.
- Enter the second number.
- View the result.
- Decide to continue with the result or start a new calculation.
from art import logo
#
from art import logo
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):
return n1 / n2
def power(n1, n2):
return n1**n2
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
"**": power,
}
def calculator():
print(logo)
num1 = float(input("What's the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = float(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(
f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: "
== "y"
):
num1 = answer
else:
should_continue = False
calculator()
calculator()
calculator()