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

Commit bc5cce3

Browse files
authored
Merge pull request #150 from muskangoyal06/master
Updated Simple_Calculator.py
2 parents e8260fe + 5c22945 commit bc5cce3

File tree

1 file changed

+125
-29
lines changed

1 file changed

+125
-29
lines changed

Basic-Scripts/Simple_calculator.py

+125-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,125 @@
1-
operation = input('''
2-
Please type in the math operation you would like to complete:
3-
1 for addition
4-
2 for subtraction
5-
3 for multiplication
6-
4 for division
7-
''')
8-
9-
num = int(input('Enter your first number: '))
10-
num2 = int(input('Enter your second number: '))
11-
12-
if operation == '1':
13-
print('{} + {} = '.format(num, num2))
14-
print(num+num2)
15-
16-
elif operation == '2':
17-
print('{} - {} = '.format(num, num2))
18-
print(num-num2)
19-
20-
elif operation == '3':
21-
print('{} * {} = '.format(num, num2))
22-
print(num*num2)
23-
24-
elif operation == '4':
25-
print('{} / {} = '.format(num, num2))
26-
print(num / num2)
27-
28-
else:
29-
print('Wrong Input! Please Try Again')
1+
2+
3+
"""The previous script becomes very boring for the user as he has to keep on typing just numbers and of wants to interact using
4+
simple english language he is unable to do so. Also the program terminates after one execution and if the user wants to keep on
5+
performing operations it becomes difficult.
6+
I have tried making a more simple and interactive calculator which allows the user to input any statement for performing the tasks. """
7+
8+
9+
10+
11+
def Calculator(num_1,num_2):
12+
#Executing the loop infinite times as we donot know how many times the user will want to run
13+
while(True):
14+
choice= input("Enter what you want to perform: ")
15+
print()
16+
17+
#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
18+
if ("addition" in choice) or ("add" in choice) or ("sum" in choice) or ("plus" in choice):
19+
sum = (num_1) + (num_2)
20+
print("Output....")
21+
print("Adding {} and {} results to {}".format(num_1,num_2,sum))
22+
print()
23+
24+
#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
25+
elif ("subtraction" in choice) or ("minus" in choice) or ("difference" in choice) or ("subtract" in choice):
26+
if( num_1 > num_2 ):
27+
diff = (num_1) - (num_2)
28+
print("Output....")
29+
print("Subtracting {} from {} results to {}".format(num_2,num_1,diff))
30+
print()
31+
elif( num_2 > num_1 ):
32+
diff = (num_2) - (num_1)
33+
print("Output....")
34+
print("Subtracting {} from {} results to {}".format(num_1,num_2,diff))
35+
print()
36+
37+
#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
38+
elif ("multiplication" in choice) or ("product" in choice) or ("multiply" in choice):
39+
if(num_1==0 or num_2==0):
40+
print("Output....")
41+
print("Multiplying {} and {} results to 0".format(num_1,num_2))
42+
print()
43+
elif(num_1==1):
44+
print("Output....")
45+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_2))
46+
print()
47+
elif(num_2==1):
48+
print("Output....")
49+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_1))
50+
print()
51+
else:
52+
mul = (num_1) * (num_2)
53+
print("Output....")
54+
print("Multiplying {} and {} results to {}".format(num_1,num_2,mul))
55+
print()
56+
57+
#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
58+
elif("division" in choice) or ("divide" in choice) or ("quotient" in choice):
59+
if( num_1 > num_2 ):
60+
if(num_2==0):
61+
print("Output....")
62+
print("Error: Please try with some other values!")
63+
64+
elif(num_1==0):
65+
print("Output....")
66+
print("Dividing {} from {} results to 0".format(num_1,num_2))
67+
print()
68+
else:
69+
div = (num_1) / (num_2)
70+
print("Output....")
71+
print("Dividing {} from {} results to {}".format(num_1,num_2,div))
72+
print()
73+
elif(num_1==0 and num_2==0):
74+
print("Infinity!")
75+
print()
76+
elif( num_2 > num_1 ):
77+
if(num_1==0):
78+
print("Output....")
79+
print("Error: Please try with some other values!")
80+
print()
81+
82+
elif(num_2==0):
83+
print("Output....")
84+
print("Dividing {} from {} results to 0".format(num_1,num_2))
85+
print()
86+
else:
87+
div = (num_2) / (num_1)
88+
print("Output....")
89+
print("Dividing {} from {} results to {}".format(num_2,num_1,div))
90+
print()
91+
92+
#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
93+
elif ("exit" in choice) or ("stop" in choice) or ("return" in choice):
94+
break
95+
96+
else:
97+
print()
98+
print("Operation not found: Please try again!")
99+
print()
100+
101+
102+
103+
def main():
104+
105+
print()
106+
print(" THIS IS A BASIC USER FRIENDLY CALCULATOR! ")
107+
print()
108+
print("You can type a sentence and interact.")
109+
print()
110+
#inputting two numbers at a time using the split function
111+
num_1,num_2 = input("Enter two numbers: ").split()
112+
num1=float(num_1)
113+
num2=float(num_2)
114+
115+
116+
#printing both the numbers
117+
print("Number 1 is: ",num_1)
118+
print("Number 2 is: ",num_2)
119+
print()
120+
121+
Calculator(num_1,num_2)
122+
123+
124+
if __name__ == "__main__":
125+
main()

0 commit comments

Comments
 (0)