This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathSimple_calculator.py
128 lines (105 loc) · 5.28 KB
/
Simple_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
"""The previous script becomes very boring for the user as he has to keep on typing just numbers and of wants to interact using
simple english language he is unable to do so. Also the program terminates after one execution and if the user wants to keep on
performing operations it becomes difficult.
I have tried making a more simple and interactive calculator which allows the user to input any statement for performing the tasks. """
def Calculator(num_1,num_2):
#Executing the loop infinite times as we donot know how many times the user will want to run
while(True):
choice= input("Enter what you want to perform: ")
print()
#For Addition user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words
if ("addition" in choice) or ("add" in choice) or ("sum" in choice) or ("plus" in choice):
sum = (num_1) + (num_2)
print("Output....")
print("Adding {} and {} results to {}".format(num_1,num_2,sum))
print()
#For Subtraction user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words
elif ("subtraction" in choice) or ("minus" in choice) or ("difference" in choice) or ("subtract" in choice):
if( num_1 > num_2 ):
diff = (num_1) - (num_2)
print("Output....")
print("Subtracting {} from {} results to {}".format(num_2,num_1,diff))
print()
elif( num_2 > num_1 ):
diff = (num_2) - (num_1)
print("Output....")
print("Subtracting {} from {} results to {}".format(num_1,num_2,diff))
print()
#For Multiplication user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words
elif ("multiplication" in choice) or ("product" in choice) or ("multiply" in choice):
if(num_1==0 or num_2==0):
print("Output....")
print("Multiplying {} and {} results to 0".format(num_1,num_2))
print()
elif(num_1==1):
print("Output....")
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_2))
print()
elif(num_2==1):
print("Output....")
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_1))
print()
else:
mul = (num_1) * (num_2)
print("Output....")
print("Multiplying {} and {} results to {}".format(num_1,num_2,mul))
print()
#For Division user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words
elif("division" in choice) or ("divide" in choice) or ("quotient" in choice):
if( num_1 > num_2 ):
if(num_2==0):
print("Output....")
print("Error: Please try with some other values!")
elif(num_1==0):
print("Output....")
print("Dividing {} from {} results to 0".format(num_1,num_2))
print()
else:
div = (num_1) / (num_2)
print("Output....")
print("Dividing {} from {} results to {}".format(num_1,num_2,div))
print()
elif(num_1==0 and num_2==0):
print("Infinity!")
print()
elif( num_2 > num_1 ):
if(num_1==0):
print("Output....")
print("Error: Please try with some other values!")
print()
elif(num_2==0):
print("Output....")
print("Dividing {} from {} results to 0".format(num_1,num_2))
print()
else:
div = (num_2) / (num_1)
print("Output....")
print("Dividing {} from {} results to {}".format(num_2,num_1,div))
print()
#For exiting the loop user can type any Sentence containing word related to it and it will exit from the loop but here we are checking for the common words
elif ("exit" in choice) or ("stop" in choice) or ("return" in choice):
break
else:
print()
print("Operation not found: Please try again!")
print()
def main():
print()
print(" THIS IS A BASIC USER FRIENDLY CALCULATOR! ")
print()
print("The allowed actions are:")
print(" 1) ADDITION \n 2) SUBTRACTION \n 3) MULTIPLICATION \n 4) DIVISION")
print()
print("You can type a sentence and interact.")
print()
#inputting two numbers at a time using the split function
num_1,num_2 = input("Enter two numbers: ").split()
num_1=float(num_1)
num_2=float(num_2)
#printing both the numbers
print("Number 1 is: ",num_1)
print("Number 2 is: ",num_2)
print()
Calculator(num_1,num_2)
if __name__ == "__main__":
main()