为 python 计算器求平方根

Making a square root for python calculator

我是 Python 的新手,正在用它写一个工程计算器。但我目前无法创建平方根函数。例如 - 这个显示错误 "TypeError: unsupported operand type(s) for /: 'int' and 'str' "。您对如何使其正常工作有任何想法吗?

    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)

问题正是错误所说的:您将 str ("") 作为 number 传递给 calculus.square_root。将其更改为 intfloat,以便可以计算 1/number。此外,您可能想要检查 number 等于 0 的情况,因为除以零将导致 ZeroDivisionError.