Created a recursive basic algebra calculator!
Focuses: - Functions with outputs - Functions with dictionaries - Basic recursion
This commit is contained in:
parent
550cce39fd
commit
e1e59f5c08
2 changed files with 64 additions and 0 deletions
16
Day 10/art.py
Normal file
16
Day 10/art.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
logo = """
|
||||
_____________________
|
||||
| _________________ |
|
||||
| | Pythonista 0. | | .----------------. .----------------. .----------------. .----------------.
|
||||
| |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. |
|
||||
| ___ ___ ___ ___ | | | ______ | || | __ | || | _____ | || | ______ | |
|
||||
| | 7 | 8 | 9 | | + | | | | .' ___ | | || | / \ | || | |_ _| | || | .' ___ | | |
|
||||
| |___|___|___| |___| | | | / .' \_| | || | / /\ \ | || | | | | || | / .' \_| | |
|
||||
| | 4 | 5 | 6 | | - | | | | | | | || | / ____ \ | || | | | _ | || | | | | |
|
||||
| |___|___|___| |___| | | | \ `.___.'\ | || | _/ / \ \_ | || | _| |__/ | | || | \ `.___.'\ | |
|
||||
| | 1 | 2 | 3 | | x | | | | `._____.' | || ||____| |____|| || | |________| | || | `._____.' | |
|
||||
| |___|___|___| |___| | | | | || | | || | | || | | |
|
||||
| | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' |
|
||||
| |___|___|___| |___| | '----------------' '----------------' '----------------' '----------------'
|
||||
|_____________________|
|
||||
"""
|
||||
48
Day 10/main.py
Normal file
48
Day 10/main.py
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue