Created a recursive basic algebra calculator!

Focuses:
- Functions with outputs
- Functions with dictionaries
- Basic recursion
This commit is contained in:
Mueez Khan 2021-01-12 23:24:45 -05:00 committed by GitHub
parent 550cce39fd
commit e1e59f5c08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions

48
Day 10/main.py Normal file
View file

@ -0,0 +1,48 @@
from art import logo
#Calculator
# Add
def add(n1, n2):
return n1 + n2
# Subtract
def subtract(n1, n2):
return n1 - n2
# Multiply
def multiply(n1, n2):
return n1 * n2
# Divide
def divide(n1, n2):
return n1 / n2
# Calculator dict
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
}
def calculator():
print(logo)
num1 = float(input("What's the first number?: "))
num2 = float(input("What's the second number?: "))
for symbol in operations:
print(symbol)
operation_symbol = input("Pick an operation: ")
answer = operations[operation_symbol](num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
check = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to exit.: ")
while check == "y":
operation_symbol = input("Pick another operation: ")
old_num = answer
num3 = float(input("What's the next number?: "))
answer = operations[operation_symbol](answer, num3)
print(f"{old_num} {operation_symbol} {num3} = {answer}")
check = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to exit.: ")
calculator()
calculator()