-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_calculator.py
More file actions
64 lines (48 loc) · 1.44 KB
/
basic_calculator.py
File metadata and controls
64 lines (48 loc) · 1.44 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
# Define the functions needed: add, sub, mul, div
# Print options to the user
# Ask for values
# Call the functions
# While loop to continue the program until the user wants to exit
def add(num1, num2):
return print("Result:", num1 + num2)
def sub(num1, num2):
return print("Result:", num1 - num2)
def mul(num1, num2):
return print("Result:", num1 * num2)
def div(num1, num2):
return print("Result:", num1 / num2)
operators = {
"+": "Addition",
"-": "Subtraction",
"*": "Multiplication",
"/": "Division",
"q": "Quit"
}
def options():
print("")
for key in operators:
print(key, ":", operators[key])
print("")
if __name__ == "__main__":
while True:
try:
options()
operator = input("Choose an option: ").lower()
if operator == "" or operator == "q":
break
elif operator not in operators:
raise Exception
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
if operator == "+":
add(num1, num2)
elif operator == "-":
sub(num1, num2)
elif operator == "*":
mul(num1, num2)
elif operator == "/":
div(num1, num2)
except ZeroDivisionError:
print("Please, don't do weird stuff")
except Exception:
print("Invalid")