python - 无法使代数(二次公式)起作用

标签 python calculator algebra quadratic

我一直在为计算机科学类(class)开发具有一些函数的非常简单的计算器,其中计算器的函数是求解二次方程。我试图让它工作,但我收到此错误:screenshot of error .我不明白为什么会这样,因为我输入了所有内容。 screenshot of input .如果有人可以提供帮助,那就太好了。

import tkinter as tk
import math
from functools import partial

#defining functions for operations
#Addition
def add_result(label_result, n1, n2):
    num1 = (n1.get())
    num2 = (n2.get())
    result = float(num1)+float(num2)
    label_result.config(text="Result is %d" % result)
    return

#Multiplication
def mult_result(label_result, n1, n2):
    num1 = (n1.get())
    num2 = (n2.get())
    result = float(num1)*float(num2)
    label_result.config(text="Result is %d" % result)
    return

#Subtraction
def sub_result(label_result, n1, n2):
    num1 = (n1.get())
    num2 = (n2.get())
    result = float(num1)-float(num2)
    label_result.config(text="Result is %d" % result)
    return

#Division
def div_result(label_result, n1, n2):
    num1 = (n1.get())
    num2 = (n2.get())
    result = float(num1)/float(num2)
    label_result.config(text="Result is %d" % result)
    return

#SquareRoot
def sqrt_result(label_result, n1):
    num1 = (n1.get())
    result = math.sqrt(float(num1))
    label_result.config(text="Result is %d" % result)
    return

def algebraMode(label_result, n1, n2, n3):
    num1 = (n1.get())
    num2 = (n2.get())
    num3 = (n3.get())
    sol1 = (-float(num2) + math.sqrt(float(num2)**2 - 4*float(num1)*float(num3))) / (2 * float(num1))
    sol2 = (-float(num2) - math.sqrt(float(num2)**2 - 4*float(num1)*float(num3))) / (2 * float(num1))
    label_result.config(text="The solutions are %d" % sol1 + " %d" % sol2)
    return



root = tk.Tk()
root.geometry('450x170+100+200')
root.title('Simple Calculator')

number1 = tk.StringVar()
number2 = tk.StringVar()
number3 = tk.StringVar()


labelNum1 = tk.Label(root, text="Enter a number").grid(row=2, column=1)
labelNum2 = tk.Label(root, text="Enter another number").grid(row=3, column=1)
labelNum3 = tk.Label(root, text="Enter another number (Algebra Mode Only)").grid(row=4, column=1)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)

entryNum1 = tk.Entry(root, textvariable=number1).grid(row=2, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=3, column=2)
entryNum3 = tk.Entry(root, textvariable=number3).grid(row=4, column=2)
add_result = partial(add_result, labelResult, number1, number2)
mult_result = partial(mult_result, labelResult, number1, number2)
sub_result = partial(sub_result, labelResult, number1, number2)
div_result = partial(div_result, labelResult, number1, number2)
sqrt_result = partial(sqrt_result, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Add", command=add_result).grid(row=1, column=0)
buttonCal = tk.Button(root, text="Multiply", comma=mult_result).grid(row=2, column=0)
buttonCal = tk.Button(root, text="Subtract", command=sub_result).grid(row=3, column=0)
buttonCal = tk.Button(root, text="Divide", command=div_result).grid(row=4, column=0)
buttonCal = tk.Button(root, text="Square Root", command=sqrt_result).grid(row=5, column=0)
buttonCal = tk.Button(root, text="Algebra", command=algebraMode).grid(row=6, column=0)
root.mainloop()

最佳答案

sqrt_result = partial(....) 下方添加以下代码:

algebraMode = partial(algebraMode, labelResult, number1, number2, number3)

然后它应该像您的其他人一样工作。

关于python - 无法使代数(二次公式)起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51508771/

相关文章:

math - 帮助理解单极传递函数

用于代数简化和求解的 C# 库

python - python 中的模块 `operator` 没有 `rfloordiv`?

python - Tensorflow 对象检测示例 : TypeError: '<' not supported between instances of 'tuple' and 'str'

python - 如何区分 lambda 和 def 函数?

java - 插入字符串的某些部分

javascript - 范围 slider 计算器

java - 将 Jbutton 添加到 Jpanel

ruby - Ruby 中的统计和矩阵代数

python - NumPy bitwise_and 函数的简化