-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24_Calculator.py
203 lines (178 loc) · 7.01 KB
/
24_Calculator.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
print("<<<<<-----------Calculator----------->>>>>")
# function for addition
def addition(a, b):
return (a + b)
# function for subtraction
def subtraction(a, b):
return (a - b)
# function for multiplication
def multiplication(a, b):
return (a * b)
# function for division
def division(a, b):
# division by zero is not possible
if (b == 0):
print("Division by zero is not possible!!!!!")
else:
return (a / b)
# function for modulo
def modulo(a, b):
# division by zero is not possible
if (b == 0):
print("Division by zero is not possible!!!!!")
else:
return (a % b)
def squre(a):
return a*a
def cube(a):
return a*a*a
def sqroot(a):
if (a== 0):
print("Can not calculate squreroot of ",a)
return (a**(1/2))
def cuberoot(a):
if (a== 0):
print("Can not calculate cuberoot of ",a)
return (a**(1/3))
print("-----------------------------------------------")
print("Enter i if you want to input a integer value")
print("Enter f if you want to input a float value")
print("-----------------------------------------------")
print("Enter '+' for addition")
print("Enter '-' for subtraction")
print("Enter '*' for multipication ")
print("Enter '/' for division ")
print("Enter '%' for modulas")
print("Enter sq for squre")
print("Enter sqr for squre root")
print("Enter cu for cube")
print("Enter cur for cuberoot")
print("-----------------------------------------------")
choice1 = str(input("Enter the choice for integer or float(i or I or f or F): "))
choice2 = str(input("Enter your choice for arthmetic operation(+,/,*,-,%): "))
print("-----------------------------------------------")
# if user chooses i or I then this condition is true
if(choice1 == 'i' or choice1 == 'I'):
choice3 = input("If you want to perform Arthmetic operation then Enter Ar ")
if (choice3 =='Ar' or choice3 == 'ar'):
a1 = (input("Enter the first integer: "))
# checking for invalid input using ascii values
# for loop is used to check if there is a invalid input given by the user
# for loop is used to check multiple characters
for i in a1:
# the ascii values of 0 to 9 is from 48 to 57 so except these values all the other values are invalid to be considered as a integer
# '.'(ascii value = 46) is invalid for a integer but valid for float
if(0 <= ord(i) and 47 >= ord(i)):
# one thing to keep in mind is that '-'(ascii value = 45) and '+'(ascii value = 43) can also be accepted as integer & float if they are at correct position i.e. at the beginning of any number for example: -8,-96,+96 are all valid but 8-,96+ are invalid
if(a1[0] == '-' or a1[0] == '+'):
# if the above condition is true then use the continue statement
if(ord(i) == 45 or ord(i) == 43):
continue
print("Invalid Input!!!")
exit()
elif(58 <= ord(i) and 127 >= ord(i)):
# if this condition is true the exit the program
print("Invalid Input!!!")
exit()
# convert the valid string into integer
a1 = int(a1)
# the same process goes for second variable
b1 = (input("Enter the second integer: "))
print("-----------------------------------------------")
for j in b1:
if(0 <= ord(j) and 47 >= ord(j)):
if(b1[0] == '-' or b1[0] == '+'):
if(ord(j) == 45 or ord(j) == 43):
continue
print("Invalid Input!!!")
exit()
elif(58 <= ord(j) and 127 >= ord(j)):
print("Invalid Input!!!")
exit()
b1 = int(b1)
# the choice for arthmetic operation is given by the user so use the appropriate function according to the operator
if(choice2 == '+'):
print(a1, "+", b1, "=", addition(a1, b1))
elif(choice2 == '-'):
print(a1, "-", b1, "=", subtraction(a1, b1))
elif(choice2 == '*'):
print(a1, "*", b1, "=", multiplication(a1, b1))
elif(choice2 == '/'):
# this condition is for zero division
if(b1 == 0):
division(a1, b1)
else:
print(a1, "/", b1, "=", division(a1, b1))
elif(choice2 == '%'):
# this condition is for zero division
if(b1 == 0):
modulo(a1, b1)
else:
print(a1, "%", b1, "=", modulo(a1, b1))
elif (choice2 == 'sq'):
a= float(input("Enter number to get Squre"))
print("Squre of ", a ," is " ,squre(a))
elif (choice2 == 'sqr'):
a= float(input("Enter number to get Squre root"))
if (a== 0):
print("Can not calculate squreoot of ",a)
print("Squreroot of ", a ," is " ,sqroot(a))
elif (choice2 == 'cube'):
a= float(input("Enter number to get Cube"))
print("Cube of ", a ," is " ,cube(a))
elif (choice2 == 'cur'):
a= float(input("Enter number to get Cuberoot"))
if (a== 0):
print("Can not calculate cuberoot of ",a)
print("Cuberoot of ", a ," is " ,cuberoot(a))
# if user chooses f or F then this condition is true
# all the conditions is same as of integer just keep in mind that '.'(ascii value = 46) is valid in float
elif(choice1 == 'f' or choice1 == 'F'):
a2 = (input("Enter the first integer: "))
for k in a2:
if(0 <= ord(k) and 47 >= ord(k)):
if(a2[0] == '-' or a2[0] == '+'):
if(ord(k) == 45 or ord(k) == 43):
continue
if(ord(k) == 46):
continue
print("Invalid Input!!!")
exit()
elif(58 <= ord(k) and 127 >= ord(k)):
print("Invalid Input!!!")
exit()
a2 = float(a2)
b2 = (input("Enter the second integer: "))
print("-----------------------------------------------")
for l in b2:
if(0 <= ord(l) and 47 >= ord(l)):
if(b2[0] == '-' or b2[0] == '+'):
if(ord(l) == 45 or ord(l) == 43):
continue
if(ord(l) == 46):
continue
print("Invalid Input!!!")
exit()
elif(58 <= ord(l) and 127 >= ord(l)):
print("Invalid Input!!!")
exit()
b2 = float(b2)
if(choice2 == '+'):
print(a2, "+", b2, "=", addition(a2, b2))
elif(choice2 == '-'):
print(a2, "-", b2, "=", subtraction(a2, b2))
elif(choice2 == '*'):
print(a2, "*", b2, "=", multiplication(a2, b2))
elif(choice2 == '/'):
if(b2 == 0):
division(a2, b2)
else:
print(a2, "/", b2, "=", division(a2, b2))
elif(choice2 == '%'):
if(b2 == 0):
modulo(a2, b2)
else:
print(a2, "%", b2, "=", modulo(a2, b2))
# if user nither chooses i or I or f or F then print a message
else:
print("Invalid Input!!!")