-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramming a Calculator using Python.py
More file actions
129 lines (110 loc) · 3.09 KB
/
Copy pathProgramming a Calculator using Python.py
File metadata and controls
129 lines (110 loc) · 3.09 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#TODO: Write the functions for arithmatic operations here
#These functions should cover Task 2
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
try:
return a / b
except Exception as e:
print(e)
return None
def power(a, b):
return a ** b
def remainder(a, b):
try:
return a % b
except Exception as e:
print(e)
return None
#-------------------------------------
#TODO: Write the select_op(choice) function here
#This function sould cover Task 1 (Section 2) and Task 3
def select_op(choice):
# terminate
if choice == "#":
return -1
# reset
if choice == "$":
return 0
# valid arithmetic operators
if choice not in ['+','-','*','/','^','%']:
print("Unrecognized operation")
return 0
# ----- INPUT FIRST NUMBER -----
while True:
num1 = input("Enter first number: ")
print(num1)
if num1 == "$":
return 0
if num1 == "#":
return -1
if num1.endswith("$"):
return 0
if num1.endswith("#"):
return -1
try:
num1 = float(num1)
break
except:
print("Not a valid number,please enter again")
# ----- INPUT SECOND NUMBER -----
while True:
num2 = input("Enter second number: ")
print(num2)
if num2 == "$":
return 0
if num2 == "#":
return -1
if num2.endswith("$"):
return 0
if num2.endswith("#"):
return -1
try:
num2 = float(num2)
break
except:
print("Not a valid number,please enter again")
# ----- PERFORM OPERATION -----
result = None
try:
if choice == '+':
result = add(num1, num2)
elif choice == '-':
result = subtract(num1, num2)
elif choice == '*':
result = multiply(num1, num2)
elif choice == '/':
result = divide(num1, num2)
elif choice == '^':
result = power(num1, num2)
elif choice == '%':
result = remainder(num1, num2)
except:
print("Something Went Wrong")
print(f"{float(num1)} {choice} {float(num2)} = {result}")
return 0
#End the select_op(choice) function here
#-------------------------------------
#This is the main loop. It covers Task 1 (Section 1)
#YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE
while True:
print("Select operation.")
print("1.Add : + ")
print("2.Subtract : - ")
print("3.Multiply : * ")
print("4.Divide : / ")
print("5.Power : ^ ")
print("6.Remainder: % ")
print("7.Terminate: # ")
print("8.Reset : $ ")
# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
print(choice)
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
exit()