Repository navigation
Expand file tree
/
Copy pathcalculator
More file actions
76 lines (54 loc) · 2.12 KB
/
Copy pathcalculator
File metadata and controls
76 lines (54 loc) · 2.12 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from calc_art import logo
print(logo)
# My Solution
def add(n1,n2):
""" Function to add two numbers
"""
return n1 + n2
def subtract(n1,n2):
""" Function to subtract two numbers
"""
return n1 - n2
def multiply(n1,n2):
""" Function to multiply two numbers
"""
return n1 * n2
def divide(n1,n2):
""" Function to divide two numbers
"""
return n1 / n2
operations = {"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
def calculator():
valid_choices = ['y','n']
num1 = float(input("What's the first number?: "))
for symbol in operations:
print(symbol)
operation_symbol = input("Pick an operation from the line above: ")
num2 = float(input("What's the next number?: "))
for key, val in operations.items():
if key == operation_symbol:
first_answer = val(num1,num2)
print(f"{num1} {operation_symbol} {num2} = {first_answer}")
ask_again = input(f"Type 'y' to continue calculating with {first_answer}, or type 'n' to start a new calculation: ")
while ask_again not in valid_choices:
ask_again = input(f"Those were not valid choices. Type 'y' to continue calculating with {first_answer}, or type 'n' to start a new calculation: ")
if ask_again == 'n':
calculator()
while ask_again == 'y':
operation_symbol = input("Pick another operation: ")
num = float(input("What's the next number?: "))
for key, val in operations.items():
if key == operation_symbol:
recent_answer = val(first_answer, num)
print(f"{first_answer} {operation_symbol} {num} = {recent_answer}")
first_answer = recent_answer
ask_again = input(f"Type 'y' to continue calculating with {recent_answer}, or type 'n' to start a new calculation: ")
while ask_again not in valid_choices:
ask_again = input(f"Those were not valid choices. Type 'y' to continue calculating with {recent_answer}, or type 'n' to start a new calculation: ")
if ask_again == 'n':
calculator()
calculator()