Calculator version 2
Introducing the updated version of our calculator powered by AI Python. This advanced calculator utilizes artificial intelligence to provide accurate calculations in a fraction of the time.
Features:
- AI-powered calculations
- Improved accuracy
- Enhanced user experience
How it works:
Our calculator version 2 is built using Python programming language with AI capabilities. This allows the calculator to understand complex mathematical expressions and provide quick and accurate results.
Benefits:
- Save time on calculations
- Reduce errors in calculations
- Improve productivity
Get started:
Try out our calculator version 2 today and experience the power of AI Python in your calculations. Simply enter your mathematical expression and let the AI do the rest!
I see now how Geordi La Forge was able to program the Enterprise so easily! The computer doing its own programming.
Here's the code.
import tkinter as tk
import math
class ScientificCalculator:
def __init__(self, master):
self.master = master
master.title("Scientific Calculator")
master.configure(bg='silver')
self.display = tk.Entry(master, font=("Arial", 24), width=30, justify='right', bd=10, relief="sunken", bg='white')
self.display.grid(row=0, column=0, columnspan=5, padx=10, pady=10)
self.buttons = {}
self.create_buttons()
self.radians = True
def create_buttons(self):
buttons = [
'C', '(', ')', '/', 'sqrt',
'7', '8', '9', '*', 'x^y',
'4', '5', '6', '-', 'sin',
'1', '2', '3', '+', 'cos',
'0', '.', '=', 'tan', '1/x',
'arcsin', 'arccos', 'arctan', 'Rad', 'exp(x)',
'Ï€', '+/-', '%', 'ln', 'log10', 'x^2'
]
row = 1
col = 0
for i, button in enumerate(buttons):
command = lambda x=button: self.click(x)
self.buttons[button] = tk.Button(self.master, text=button, font=("Arial", 18), command=command, height=2, width=5, bd=5, relief="raised", bg='lightgray')
self.buttons[button].grid(row=row, column=col, sticky='nsew', padx=5, pady=5)
col += 1
if col > 4:
col = 0
row += 1
def click(self, key):
if key == '=':
try:
expression = self.display.get()
if '%' in expression:
nums = expression.split('%')
result = float(nums[0]) * float(nums[1]) / 100
else:
result = eval(expression)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'C':
self.display.delete(0, tk.END)
elif key == '+/-':
try:
current_value = float(self.display.get())
new_value = -current_value
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(new_value))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'x^2':
try:
value = float(self.display.get())
result = value ** 2
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key in ['sin', 'cos', 'tan']:
try:
value = float(self.display.get())
if not self.radians:
value = math.radians(value)
result = getattr(math, key)(value)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key in ['arcsin', 'arccos', 'arctan']:
try:
value = float(self.display.get())
if key in ['arcsin', 'arccos'] and (value < -1 or value > 1):
raise ValueError("Input out of range for arcsin/arccos")
result = getattr(math, 'a' + key[3:])(value)
if not self.radians:
result = math.degrees(result)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except Exception as e:
self.display.delete(0, tk.END)
self.display.insert(tk.END, f"Error: {str(e)}")
elif key == 'sqrt':
try:
result = math.sqrt(float(self.display.get()))
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'x^y':
self.display.insert(tk.END, '**')
elif key == 'exp':
self.display.insert(tk.END, 'math.exp(')
elif key == '1/x':
try:
result = 1 / float(self.display.get())
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'Rad':
self.radians = not self.radians
self.buttons['Rad'].config(text="Rad" if self.radians else "Deg")
elif key == 'exp(x)':
try:
value = float(self.display.get())
result = math.exp(value)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'Ï€':
self.display.insert(tk.END, str(math.pi))
elif key == 'ln':
try:
value = float(self.display.get())
result = math.log(value)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
elif key == 'log10':
try:
value = float(self.display.get())
result = math.log10(value)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
except:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Error")
else:
self.display.insert(tk.END, key)
root = tk.Tk()
for i in range(1, 7):
root.grid_rowconfigure(i, weight=1)
for i in range(5):
root.grid_columnconfigure(i, weight=1)
calculator = ScientificCalculator(root)
root.mainloop()