Functions with Outputs
-
return
keyword to output and store the result
-docstrings: right after the def line; """description of function"""
End of the lesson project: Calculator
from art import logo
print(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
operations = {"+": add, "-": subtract, "*": multiply, "/": divide}
def calculator():
num1 = float(input("What's the first number?: "))
for item in operations:
print(item)
should_continue = True
while should_continue == True:
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()
Top comments (0)