Reputation: 304
I am new to Python and writing an engineering calculator on it. But i can't make a square root function currently. For example - this one show me an error "TypeError: unsupported operand type(s) for /: 'int' and 'str' ". Do you have any ideas how to make it work properly?
def square_root(self, number=1):
e.delete(0, END)
self.value = float(self.value ** (1/number))
e.insert(0, self.value)
button_root = Button(gui, text="√", padx=40, pady=40, command=lambda: calculus.square_root(""))
button_root.grid(row=2, column=5)
Upvotes: 0
Views: 931
Reputation: 534
The problem is exactly what the error says: you're passing a str
(""
) as your number
for calculus.square_root
. Change this to an int
or float
so that 1/number
can be evaluated. Also, you might want to check for cases when number
equals 0, as dividing by zero will lead to a ZeroDivisionError
.
Upvotes: 2