-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion_calculator.py
More file actions
61 lines (34 loc) · 1.25 KB
/
recursion_calculator.py
File metadata and controls
61 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Functions for mathematical operators
def add(n1, n2):
return n1 + n2
def sub(n1, n2):
return n1 - n2
def mul(n1, n2):
return n1 * n2
def div(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": sub,
"*": mul,
"/": div,
}
# Function to calculate the inputs given from the user
def calculate():
# Ask user what they would like to calculate
num_1 = int(input("\nWhat's the first number? "))
# Create a loop to continue calculating with same answer
while True:
operator = input('\n"+" "-" "*" "/"\nPick an operation: ')
num_2 = int(input("\nWhat's the second number? "))
answer = operations[operator](num_1, num_2)
print(f"{num_1} {operator} {num_2} = {answer}\n")
# Check if user would like to continue calculating with initial calculation
status = input('Continue calculating with {answer}? Type "Y". OR Type "N" to begin a new calculation: ').upper().replace(" ", "")
if status == "Y":
num_1 = answer
elif status == "N":
print("\nCalculation has ended. Starting a new Calculation.")
# Call function to repeat itself (i.e. Recursion)
calculate()
calculate()