Skip to content

Commit a93f118

Browse files
Merge pull request steam-bell-92#388 from Suhani-ai-dev/calculator-upgrade
feat: upgraded calculator UI with scientific functions
2 parents bb80d2f + c3f1c1e commit a93f118

2 files changed

Lines changed: 440 additions & 267 deletions

File tree

Lines changed: 222 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,275 @@
11
import math
22

3-
print("🧮 Simple Scientific Calculator 🧮")
4-
print("Perform arithmetic and scientific operations\n")
3+
print("🧮 Advanced Scientific Calculator 🧮")
4+
print("📚 Perform arithmetic and scientific operations\n")
55

66
history = []
77

88
while True:
9-
print("=" * 40)
10-
print("📋 Choose an operation:")
11-
print("1️⃣ Addition (+)")
12-
print("2️⃣ Subtraction (-)")
13-
print("3️⃣ Multiplication (×)")
14-
print("4️⃣ Division (÷)")
15-
print("5️⃣ Modulus (%)")
16-
print("6️⃣ Power (^)")
17-
print("7️⃣ Square Root (√)")
18-
print("8️⃣ Sine (sin)")
19-
print("9️⃣ Cosine (cos)")
20-
print("🔟 Tangent (tan)")
21-
print("1️⃣1️⃣ Logarithm (log10)")
22-
print("1️⃣2️⃣ Natural Log (ln)")
23-
print("1️⃣3️⃣ Show History")
24-
print("1️⃣4️⃣ Exit")
25-
print("=" * 40)
26-
27-
choice = input("\n🎯 Enter your choice (1-14): ")
28-
29-
# Exit
30-
if choice == '14':
31-
print("\n👋 Thanks for using the calculator! Goodbye!\n")
9+
10+
print("=" * 60)
11+
print("📋 Choose an Operation:")
12+
print("1️⃣ Addition (+)")
13+
print("2️⃣ Subtraction (-)")
14+
print("3️⃣ Multiplication (×)")
15+
print("4️⃣ Division (÷)")
16+
print("5️⃣ Modulus (%)")
17+
print("6️⃣ Power (^)")
18+
print("7️⃣ Square Root (√)")
19+
print("8️⃣ Percentage")
20+
print("9️⃣ Decimal Calculation")
21+
print("🔟 Sine (sin)")
22+
print("1️⃣1️⃣ Cosine (cos)")
23+
print("1️⃣2️⃣ Tangent (tan)")
24+
print("1️⃣3️⃣ Logarithm (log)")
25+
print("1️⃣4️⃣ Factorial (!)")
26+
print("1️⃣5️⃣ Show History")
27+
print("1️⃣6️⃣ Clear History")
28+
print("1️⃣7️⃣ Delete Last History")
29+
print("0️⃣ Exit")
30+
print("=" * 60)
31+
32+
choice = input("\n🎯 Enter your choice: ")
33+
34+
# EXIT
35+
if choice == '0':
36+
print("\n👋 Thanks for using the calculator. Goodbye!\n")
3237
break
3338

34-
# Show history
35-
elif choice == '13':
39+
# SHOW HISTORY
40+
elif choice == '15':
41+
3642
print("\n📜 Calculation History:\n")
3743

3844
if len(history) == 0:
39-
print("📭 No calculations found in history.\n")
45+
print("📭 No calculations found.\n")
46+
4047
else:
4148
for item in history:
42-
print(f"➡️ {item}")
43-
print()
49+
print(f"➡️ {item}")
50+
51+
print()
52+
53+
# CLEAR HISTORY
54+
elif choice == '16':
55+
56+
history.clear()
57+
print("\n🗑️ History Cleared Successfully!\n")
58+
59+
# DELETE LAST HISTORY
60+
elif choice == '17':
61+
62+
if len(history) == 0:
63+
print("\n📭 No history to delete.\n")
64+
65+
else:
66+
deleted = history.pop()
67+
print(f"\n🗑️ Deleted: {deleted}\n")
68+
69+
# SQUARE ROOT
70+
elif choice == '7':
71+
72+
try:
73+
num = float(input("\n📥 Enter number: "))
74+
75+
if num < 0:
76+
print("\n❌ Cannot find square root of negative number!\n")
77+
continue
78+
79+
result = math.sqrt(num)
80+
81+
calculation = f"√{num} = {result}"
82+
83+
history.append(calculation)
84+
85+
print(f"\n✨ Result: {calculation}\n")
86+
87+
except ValueError:
88+
print("\n❌ Invalid input!\n")
89+
90+
# PERCENTAGE
91+
elif choice == '8':
92+
93+
try:
94+
num = float(input("\n📥 Enter number: "))
95+
96+
result = num / 100
97+
98+
calculation = f"{num}% = {result}"
99+
100+
history.append(calculation)
101+
102+
print(f"\n✨ Result: {calculation}\n")
103+
104+
except ValueError:
105+
print("\n❌ Invalid input!\n")
106+
107+
# DECIMAL CALCULATION
108+
elif choice == '9':
109+
110+
try:
111+
num1 = float(input("\n📥 Enter first decimal number: "))
112+
num2 = float(input("📥 Enter second decimal number: "))
113+
114+
result = num1 + num2
115+
116+
calculation = f"{num1} + {num2} = {result}"
117+
118+
history.append(calculation)
119+
120+
print(f"\n✨ Result: {calculation}\n")
121+
122+
except ValueError:
123+
print("\n❌ Invalid decimal input!\n")
124+
125+
# SINE
126+
elif choice == '10':
127+
128+
try:
129+
angle = float(input("\n📥 Enter angle in degrees: "))
130+
131+
result = math.sin(math.radians(angle))
132+
133+
calculation = f"sin({angle}) = {result}"
134+
135+
history.append(calculation)
136+
137+
print(f"\n✨ Result: {calculation}\n")
138+
139+
except ValueError:
140+
print("\n❌ Invalid input!\n")
141+
142+
# COSINE
143+
elif choice == '11':
144+
145+
try:
146+
angle = float(input("\n📥 Enter angle in degrees: "))
147+
148+
result = math.cos(math.radians(angle))
149+
150+
calculation = f"cos({angle}) = {result}"
151+
152+
history.append(calculation)
44153

45-
# Basic arithmetic operations (require two numbers)
154+
print(f"\n✨ Result: {calculation}\n")
155+
156+
except ValueError:
157+
print("\n❌ Invalid input!\n")
158+
159+
# TANGENT
160+
elif choice == '12':
161+
162+
try:
163+
angle = float(input("\n📥 Enter angle in degrees: "))
164+
165+
result = math.tan(math.radians(angle))
166+
167+
calculation = f"tan({angle}) = {result}"
168+
169+
history.append(calculation)
170+
171+
print(f"\n✨ Result: {calculation}\n")
172+
173+
except ValueError:
174+
print("\n❌ Invalid input!\n")
175+
176+
# LOGARITHM
177+
elif choice == '13':
178+
179+
try:
180+
num = float(input("\n📥 Enter number: "))
181+
182+
if num <= 0:
183+
print("\n❌ Logarithm only works for positive numbers!\n")
184+
continue
185+
186+
result = math.log10(num)
187+
188+
calculation = f"log({num}) = {result}"
189+
190+
history.append(calculation)
191+
192+
print(f"\n✨ Result: {calculation}\n")
193+
194+
except ValueError:
195+
print("\n❌ Invalid input!\n")
196+
197+
# FACTORIAL
198+
elif choice == '14':
199+
200+
try:
201+
num = int(input("\n📥 Enter positive integer: "))
202+
203+
if num < 0:
204+
print("\n❌ Factorial not defined for negative numbers!\n")
205+
continue
206+
207+
result = math.factorial(num)
208+
209+
calculation = f"{num}! = {result}"
210+
211+
history.append(calculation)
212+
213+
print(f"\n✨ Result: {calculation}\n")
214+
215+
except ValueError:
216+
print("\n❌ Invalid input!\n")
217+
218+
# BASIC OPERATIONS
46219
elif choice in ['1', '2', '3', '4', '5', '6']:
220+
47221
try:
48222
num1 = float(input("\n📥 Enter first number: "))
49223
num2 = float(input("📥 Enter second number: "))
50224

51225
if choice == '1':
226+
52227
result = num1 + num2
53228
calculation = f"{num1} + {num2} = {result}"
54229

55230
elif choice == '2':
231+
56232
result = num1 - num2
57233
calculation = f"{num1} - {num2} = {result}"
58234

59235
elif choice == '3':
236+
60237
result = num1 * num2
61238
calculation = f"{num1} × {num2} = {result}"
62239

63240
elif choice == '4':
241+
64242
if num2 == 0:
65-
print("\nError! Division by zero is not allowed.\n")
243+
print("\n❌ Division by zero is not allowed!\n")
66244
continue
245+
67246
result = num1 / num2
68247
calculation = f"{num1} ÷ {num2} = {result}"
69248

249+
# MODULUS
70250
elif choice == '5':
251+
71252
if num2 == 0:
72-
print("\nError! Modulus by zero is not allowed.\n")
253+
print("\n❌ Modulus by zero is not allowed!\n")
73254
continue
255+
74256
result = num1 % num2
75257
calculation = f"{num1} % {num2} = {result}"
76258

259+
# POWER
77260
elif choice == '6':
261+
78262
result = num1 ** num2
79-
calculation = f"{num1} ^ {num2} = {result}"
263+
calculation = f"{num1}^{num2} = {result}"
80264

81265
history.append(calculation)
82-
print(f"\n✨ Result: {calculation}\n")
83-
84-
except ValueError:
85-
print("\n❌ Invalid input! Please enter valid numbers.\n")
86-
87-
# Scientific operations (require one number)
88-
elif choice in ['7', '8', '9', '10', '11', '12']:
89-
try:
90-
num = float(input("\n📥 Enter a number: "))
91-
92-
if choice == '7': # Square Root
93-
if num < 0:
94-
print("\n❌ Error! Cannot calculate square root of a negative number.\n")
95-
continue
96-
result = math.sqrt(num)
97-
calculation = f"√{num} = {result}"
98-
99-
elif choice == '8': # Sine
100-
result = math.sin(math.radians(num))
101-
calculation = f"sin({num}°) = {result}"
102266

103-
elif choice == '9': # Cosine
104-
result = math.cos(math.radians(num))
105-
calculation = f"cos({num}°) = {result}"
106-
107-
elif choice == '10': # Tangent
108-
result = math.tan(math.radians(num))
109-
calculation = f"tan({num}°) = {result}"
110-
111-
elif choice == '11': # Log base 10
112-
if num <= 0:
113-
print("\n❌ Error! Logarithm is only defined for positive numbers.\n")
114-
continue
115-
result = math.log10(num)
116-
calculation = f"log10({num}) = {result}"
117-
118-
elif choice == '12': # Natural Log
119-
if num <= 0:
120-
print("\n❌ Error! Natural logarithm is only defined for positive numbers.\n")
121-
continue
122-
result = math.log(num)
123-
calculation = f"ln({num}) = {result}"
124-
125-
history.append(calculation)
126267
print(f"\n✨ Result: {calculation}\n")
127268

128269
except ValueError:
129-
print("\nInvalid input! Please enter a valid number.\n")
270+
print("\n❌ Please enter valid numbers!\n")
130271

272+
# INVALID CHOICE
131273
else:
132-
print("\n❌ Invalid choice! Please select 1-14.\n")
274+
275+
print("\n❌ Invalid choice! Please select a valid option.\n")

0 commit comments

Comments
 (0)