-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator.py
More file actions
63 lines (55 loc) · 1.87 KB
/
calculator.py
File metadata and controls
63 lines (55 loc) · 1.87 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
import os
option = 0
menu = ["Add", "Substract", "Multiply", "Divide", "Exit"]
first = 0
second = 0
def add(a, b):
return a + b
def substract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide (a, b):
return a / b
if __name__ == "__main__":
while option != len(menu) + 1:
os.system("cls")
if second == 0:
pass
elif option == 1:
print(f"{first} + {second} = {add(first, second)}\n")
elif option == 2:
print(f"{first} - {second} = {substract(first, second)}\n")
elif option == 3:
print(f"{first} * {second} = {multiply(first, second)}\n")
elif option == 4:
print(f"{first} / {second} = {divide(first, second)}\n")
else:
print("Invalid option")
for i, e in enumerate(menu):
print(f"{(i + 1)}. {e}", end=" ")
option = int(input("\nSelect an option:\n"))
first = int(input("Select a 1st number:\n"))
match option:
case 1:
second = int(input(f"Select a 2nd one: \n{first} + "))
case 2:
second = int(input(f"Select a 2nd one: \n{first} - "))
case 3:
second = int(input(f"Select a 2nd one: \n{first} * "))
case 4:
second = int(input(f"Select a 2nd one: \n{first} / "))
case _:
second = 0
if option == len(menu) + 1:
print("Bye!")
elif option == 1:
print(f"{first} + {second} = {add(first, second)}")
elif option == 2:
print(f"{first} - {second} = {substract(first, second)}")
elif option == 3:
print(f"{first} * {second} = {multiply(first, second)}")
elif option == 4:
print(f"{first} / {second} = {divide(first, second)}")
else:
print("Invalid option")