-1

I tried turning the variable that I get from input(which is (x,y,z) as ints) into a tuple and a list successfully, but I cant seem to get the Polynomial() to work with them.

from tkinter import *

class Polynomial:

    def __init__(self, *coefficients):
        self.coefficients = list(coefficients)

    def __repr__(self):

        return "Polynomial" + str(self.coefficients)

    def __call__(self, x):
        res = 0
        for index, coeff in enumerate(self.coefficients[::-1]):
            res += coeff * x ** index
        return res


def calculateIt():
    mathCoef = E1.get()
    botch = mathCoef
    mytuple = tuple(map(int, botch.split(',')))

    p = Polynomial(mytuple)

    for x in range(-3, 3):
       print(x, p(x))
    L1.configure(text=p)
    print(mathCoef)

window = Tk()
window.title("XImath Client 1.0")
window.configure(bg="black")
window.geometry("500x500")

L1 = Label(window, text="Input")
L1.pack()
E1 = Entry(window, bd=5)
E1.pack()

B1 = Button(window, text="go", command=calculateIt)
B1.pack()

window.mainloop()

The problem is that I want the class(Polynomial) to work with the variables given by user input in mathCoef, which is a string at first. I tried turning into a tuple successfully, but class(Polynomial) expects ints with this syntax - (1, 2, 3). Let's say the user inputs 1,2,3 - the tuple looks exactly like this - (1, 2, 3), but when I try passing it into Polynomial(mytuple), it returns:

TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

How do I turn the tuple into ints separated with commas?

confestim
  • 3
  • 4
  • Can you clarify what does not work about that code? Ideally so that people do not have to click through a GUI to find out? Please see the [mcve] page how to best help us help you. – MisterMiyagi Sep 22 '20 at 16:03
  • 1
    I think you're likely going to need to give a lot more description. You haven't said what the problem is, or what your expected output is. – Carcigenicate Sep 22 '20 at 16:03
  • I changed up the post, hopefully I clarified at least a little. – confestim Sep 22 '20 at 16:12
  • Why do you create a tuple, unpack the tuple, then in the class wrap the values again and finally create a list from them, when this class could just take the tuple and work with it as it is? – Wups Sep 22 '20 at 16:28

1 Answers1

0

Try the below code.

I believe what you may have been missing is the * in the line p = Polynomial(*coeffs) which tells python to unpack the list as arguments that is passes to the constructor of the class.

I've also added repr(p) to the line that adds the polynomial to the label, this will show you the repr string for that polynomial.

from tkinter import *

class Polynomial:

    def __init__(self, *coefficients):
        self.coefficients = list(coefficients)

    def __repr__(self):

        return "Polynomial" + str(self.coefficients)

    def __call__(self, x):
        res = 0
        for index, coeff in enumerate(self.coefficients[::-1]):
            res += coeff * x ** index
        return res


def calculateIt():
    mathCoef = E1.get()
    coeffs = map(int, mathCoef.split(' '))

    p = Polynomial(*coeffs)

    for x in range(-3, 3):
       print(x, p(x))
    L1.configure(text=repr(p))
    print(mathCoef)

window = Tk()
window.title("XImath Client 1.0")
window.configure(bg="black")
window.geometry("500x500")

L1 = Label(window, text="Input")
L1.pack()
E1 = Entry(window, bd=5)
E1.pack()

B1 = Button(window, text="go", command=calculateIt)
B1.pack()

window.mainloop()
scotty3785
  • 6,763
  • 1
  • 25
  • 35