-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
176 lines (128 loc) · 5.14 KB
/
calculator.py
File metadata and controls
176 lines (128 loc) · 5.14 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
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
#Importing the necessary module for advanced math operations
import math
#While loop for continuously running the program and allowing the user to always go back to the main menu and choose another option
while True:
#The user picks the math operation from the menu (we use \n to insert a new row in the menu)
print("\nChoose the math operation.\n\n0 - Addition\n1 - Subtraction\n2 - Multiplication\n3 - Division\n4 - Modulo\n5 - Raising to a power\n6 - Square root\n7 - Logarithm\n8 - Sine\n9 - Cosine\n10 - Tangent\n")
oper = input("\nYour option from the menu: ") #The variable that saves the option chosen by the user
#Addition
if oper == "0":
val1 = float(input("\nFirst value: ")) #The variable storing the first value, converted from string to float
val2 = float(input("\nSecond value: ")) #The variable storing the second value, converted from string to float
print("\nThe result is: " + str(val1 + val2) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Subtraction
elif oper == "1":
val1 = float(input("\nFirst value: "))
val2 = float(input("\nSecond value: "))
print("\nThe result is: " + str(val1 - val2) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Multiplication
elif oper == "2":
val1 = float(input("\nFirst value: "))
val2 = float(input("\nSecond value: "))
print("\nThe result is: " + str(val1 * val2) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Division
elif oper == "3":
val1 = float(input("\nFirst value: "))
val2 = float(input("\nSecond value: "))
print("\nThe result is: " + str(val1 / val2) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Modulo
elif oper == "4":
val1 = float(input("\nFirst value: "))
val2 = float(input("\nSecond value: "))
print("\nThe result is: " + str(val1 % val2) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Raising to a power
elif oper == "5":
val1 = float(input("\nEnter the base: "))
val2 = float(input("\nEnter the power: "))
print("\nThe result is: " + str(math.pow(val1, val2)) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Square root
elif oper == "6":
val1 = float(input("\nEnter value for extracting the square root: "))
print("\nThe result is: " + str(math.sqrt(val1)) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Logarithm
elif oper == "7":
val1 = float(input("\nEnter the value for calculating the logarithm to base 2: "))
print("\nThe result is: " + str(math.log(val1, 2)) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Sine
elif oper == "8":
val1 = float(input("\nEnter the value (in degrees) for calculating the sine: "))
print("\nThe result is: " + str(math.sin(math.radians(val1))) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Cosine
elif oper == "9":
val1 = float(input("\nEnter the value (in degrees) for calculating the cosine: "))
print("\nThe result is: " + str(math.cos(math.radians(val1))) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Tangent
elif oper == "10":
val1 = float(input("\nEnter the value (in degrees) for calculating the tangent: "))
print("\nThe result is: " + str(math.tan(math.radians(val1))) + "\n")
#Going back to the main menu or exiting the program
back = input('\nGo back to the main menu? (y/n) ')
if back == 'y':
continue
else:
break
#Handling invalid options
else:
print("\nInvalid option!\n")
continue
#End of the program