-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
69 lines (50 loc) · 1.69 KB
/
calculator.py
File metadata and controls
69 lines (50 loc) · 1.69 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
def add(a,b):
c = a + b
return print(f"Your answer is {c}")
def subtract(a,b):
c = a - b
return print(f"Your answer is {c}")
def divide(a,b):
c = a / b
return print(f"Your answer is {c}")
def multiply(a,b):
c = a * b
return print(f"Your answer is {c}")
def check_for_exit(input):
if input == 'e':
print("Closing calculator!")
exit()
def validate_user_int_input(int_input):
if int_input != int():
return True
else:
pass
def validate_user_operation_input(operation_input):
valid_input = ['a','s','m','d','e']
return operation_input not in valid_input
while True:
try:
user_input_1 = int(input("Please provide a number: "))
except ValueError:
print("Invalid Input! Please provide an integer.")
break
try:
user_input_2 = int(input("Please provide another number: "))
except ValueError:
print("Invalid Input! Please provide an integer.")
break
print("Enter 'e' to exit.")
user_input_operation = input("What operation do you want to perform? \
Enter 'a' for addition, 's' for subtraction, 'd' for division, 'm' for multiplication: ").lower()
check_for_exit(user_input_operation)
if validate_user_operation_input(user_input_operation) == True:
print("Please provide valid input.")
break
if user_input_operation == 'a':
add(user_input_1, user_input_2)
elif user_input_operation == 's':
subtract(user_input_1, user_input_2)
elif user_input_operation == 'd':
divide(user_input_1, user_input_2)
elif user_input_operation == 'm':
multiply(user_input_1, user_input_2)