[英]unsupported operand type(s) for ** or pow(): 'method' and 'int'
from tkinter import *
import math
import sys
def quit():
root.destroy()
def a_b_c():
print_a()
print_b()
print_c()
calculation()
return
def print_a():
get_a = a.get()
printing_a = Label(root, text=get_a).grid(row=8, column=1)
return
def print_b():
get_b = b.get()
printing_b =Label(root, text=get_b).grid(row=12, column=1)
return
def print_c():
get_c = c.get()
printing_c =Label(root, text=get_c).grid(row=16, column=1)
return
root = Tk()
a = StringVar()
b = StringVar()
c = StringVar()
root.title('Solving Quadratic Equations')
quit_b = Button(root, text="quit",command=quit).grid(row=1, column=1)
go_b = Button(root, text="go", command=a_b_c).grid(row=1, column=2)
welcome = Label(root, text="Welcome to Solving Quadratic Equations with GlaDOS",font=("Helvetica",13))
welcome.grid(row=2, column=1)
instructions = Label(root, text="So how do i use this program? is what you may ask yourself. So, for example, \n you have the equation 2x^2+5x+8=0. So the 2x^2 is a but you don't put the\n whole thing in you just but the 2 from the start in. The next thing is b\n in this case b = 5 and c is equal to 8. Once you have all you number in the boxes \n hit the go button. Remember you don't need the x's. ", font=("Helvetica",11))
instructions.grid(row=3, column=1)
line = Label(root, text="************************************************************************************************************").grid(row=4, column=1)
input_a = Label(root, text="Pls input A here", font=("Helvetica",11)).grid(row=6, column=1)
entry_a = Entry(root,textvariable=a).grid(row=7,column=1)
line = Label(root, text="************************************************************************************************************").grid(row=9, column=1)
input_b = Label(root, text="Pls input B here", font=("Helvetica",11)).grid(row=10, column=1)
entry_b = Entry(root,textvariable=b).grid(row=11,column=1)
line = Label(root, text="*************************************************************************************************************").grid(row=13, column=1)
input_c = Label(root, text="Pls input C here", font=("Helvetica",11)).grid(row=14, column=1)
entry_c = Entry(root,textvariable=c).grid(row=15,column=1)
two_a = a.get
two_b = b.get
two_c = c.get
d = two_b**2-4*two_a*two_c
def calculation():
if d < 0:
no_solution = Label(root, text="This equation has no real solution").grid(row=19, column=1)
elif d == 0:
x = (-two_b+math.sqrt(two_b**2-4*two_a*two_c))/2*two_a
one_solution = Label(root, text="This equation has one solutions: {}".format(x)).grid(row=20, column=1)
else:
x1 = (-two_b+math.sqrt((two_b**2)-(4*(two_a*two_c))))/(2*two_a)
x1 = (-two_b-math.sqrt((two_b**2)-(4*(two_a*two_c))))/(2*two_a)
two_solution= label(root, text="This equation has two solutions: {} or {} ".format(x1,x2)).grid(row=21, colum=1)
root.mainloop()
why does it say unsupported operand type(s) for ** or pow(): method or int? 为什么说**或pow():method或int的操作数类型不受支持? can someone help me to change it so it works it is for school and the teacher can't help me.
有人可以帮助我改变它,所以它适用于学校,老师不能帮助我。 I am trying to make a program that helps you to solve quadratic equations and the part at the bottom dosent work( def calculation) thanks for helping me :)
我正在尝试制作一个程序,帮助你解决二次方程和底部剂量工作(def计算)的部分感谢帮助我:)
You are assigning a few values: 您正在分配一些值:
two_a = a.get
two_b = b.get
two_c = c.get
And then doing calculations: 然后做计算:
d = two_b**2-...
However, a.get
is a method that retrieves the value of that StringVar
. 但是,
a.get
是一种检索StringVar
值的方法。 To actually call it and retrieve the value, you have to... well, call it, with parentheses: 要实际调用它并检索值,你必须......好吧,用括号来调用它:
two_a = a.get()
Furthermore, you will then have strings. 此外,您将拥有字符串。 Cast them to integers or floating-point numbers with
int
or float
: 使用
int
或float
将它们转换为整数或浮点数:
two_a = int(a.get())
# or:
two_a = float(a.get())
Then your arithmetic will work as expected. 然后你的算术将按预期工作。
Please read and study this SO help page about mcves . 请阅读并研究这个关于mcves的 SO帮助页面。 Here is an mcve based on the code you posted.
这是基于您发布的代码的mcve。 It produces the exact same error message on the last line.
它在最后一行产生完全相同的错误消息。
import tkinter as tk
root = tk.Tk()
b = tk.StringVar(root)
two_b = b.get
d = two_b**2
Raising a method to a power is nonsensical. 将方法提升为权力是荒谬的。 You need to call () the method and convert the string to a number.
您需要调用()方法并将字符串转换为数字。 Do the latter either by using DoubleVar or IntVar instead of StringVar or by passing the result of get to float() or int() before doing arithmetic on it.
通过使用DoubleVar或IntVar而不是StringVar或者在对其进行算术运算之前将get的结果传递给float()或int()来执行后者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.