Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit d2a8fb4

Browse files
committed
Update Simple_calculator.py
A better verson with a more interactive concept. To help user feel more comfortable in performing the basic operations.
1 parent 769bd50 commit d2a8fb4

File tree

1 file changed

+129
-1
lines changed

1 file changed

+129
-1
lines changed

Basic-Scripts/Simple_calculator.py

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,132 @@
2626
print(num / num2)
2727

2828
else:
29-
print('Wrong Input! Please Try Again')
29+
print('Wrong Input! Please Try Again")
30+
31+
32+
33+
34+
35+
"""The previous script becomes very boring for the user as he has to keep on typing just numbers and of wants to interact using
36+
simple english language he is unable to do so. Also the program terminates after one execution and if the user wants to keep on
37+
performing operations it becomes difficult.
38+
I have tried making a more simple and interactive calculator which allows the user to input any statement for performing the tasks. """
39+
40+
41+
42+
43+
def Calculator(num_1,num_2):
44+
#Executing the loop infinite times as we donot know how many times the user will want to run
45+
while(True):
46+
choice= input("Enter what you want to perform: ")
47+
print()
48+
49+
#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
50+
if ("addition" in choice) or ("add" in choice) or ("sum" in choice) or ("plus" in choice):
51+
sum = (num_1) + (num_2)
52+
print("Output....")
53+
print("Adding {} and {} results to {}".format(num_1,num_2,sum))
54+
print()
55+
56+
#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
57+
elif ("subtraction" in choice) or ("minus" in choice) or ("difference" in choice) or ("subtract" in choice):
58+
if( num_1 > num_2 ):
59+
diff = (num_1) - (num_2)
60+
print("Output....")
61+
print("Subtracting {} from {} results to {}".format(num_2,num_1,diff))
62+
print()
63+
elif( num_2 > num_1 ):
64+
diff = (num_2) - (num_1)
65+
print("Output....")
66+
print("Subtracting {} from {} results to {}".format(num_1,num_2,diff))
67+
print()
68+
69+
#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
70+
elif ("multiplication" in choice) or ("product" in choice) or ("multiply" in choice):
71+
if(num_1==0 or num_2==0):
72+
print("Output....")
73+
print("Multiplying {} and {} results to 0".format(num_1,num_2))
74+
print()
75+
elif(num_1==1):
76+
print("Output....")
77+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_2))
78+
print()
79+
elif(num_2==1):
80+
print("Output....")
81+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_1))
82+
print()
83+
else:
84+
mul = (num_1) * (num_2)
85+
print("Output....")
86+
print("Multiplying {} and {} results to {}".format(num_1,num_2,mul))
87+
print()
88+
89+
#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
90+
elif("division" in choice) or ("divide" in choice) or ("quotient" in choice):
91+
if( num_1 > num_2 ):
92+
if(num_2==0):
93+
print("Output....")
94+
print("Error: Please try with some other values!")
95+
96+
elif(num_1==0):
97+
print("Output....")
98+
print("Dividing {} from {} results to 0".format(num_1,num_2))
99+
print()
100+
else:
101+
div = (num_1) / (num_2)
102+
print("Output....")
103+
print("Dividing {} from {} results to {}".format(num_1,num_2,div))
104+
print()
105+
elif(num_1==0 and num_2==0):
106+
print("Infinity!")
107+
print()
108+
elif( num_2 > num_1 ):
109+
if(num_1==0):
110+
print("Output....")
111+
print("Error: Please try with some other values!")
112+
print()
113+
114+
elif(num_2==0):
115+
print("Output....")
116+
print("Dividing {} from {} results to 0".format(num_1,num_2))
117+
print()
118+
else:
119+
div = (num_2) / (num_1)
120+
print("Output....")
121+
print("Dividing {} from {} results to {}".format(num_2,num_1,div))
122+
print()
123+
124+
#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
125+
elif ("exit" in choice) or ("stop" in choice) or ("return" in choice):
126+
break
127+
128+
else:
129+
print()
130+
print("Operation not found: Please try again!")
131+
print()
132+
133+
134+
135+
def main():
136+
137+
print()
138+
print(" THIS IS A BASIC USER FRIENDLY CALCULATOR! ")
139+
print()
140+
print("You can type a sentence and interact.")
141+
print()
142+
#inputting two numbers at a time using the split function
143+
num_1,num_2 = input("Enter two numbers: ").split()
144+
num1=float(num_1)
145+
num2=float(num_2)
146+
147+
148+
#printing both the numbers
149+
print("Number 1 is: ",num_1)
150+
print("Number 2 is: ",num_2)
151+
print()
152+
153+
Calculator(num_1,num_2)
154+
155+
156+
if __name__ == "__main__":
157+
main()

0 commit comments

Comments
 (0)