Skip to main content
 首页 » 编程设计

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

2024年12月31日8langtianya

我一直在为计算机科学类(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) 

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