Hi, me again :)

Look at this error please:

   File "./kivycal-1", line 103, in number_two
     self.check()
 TypeError: check() takes exactly 2 arguments (1 given)

The error is pointing to here:

    def number_two(self, event):
        global numbers
        numbers.append(2)
        print numbers
        self.check()

This is a part of a class in a .py file. I'm coding in python and kivy in that file.
And here is the check function:

    def check(self):
        global numbers
        if len(numbers) == 3:
            ckeck_to_answer()

Well, i can't understand what's the exact problem, what should i do?

Are you sure that that's the definition of check that's in scope when you call it? Do you perhaps have any other definitions of check in your program, perhaps even one that takes two arguments?

In any case, it would help if you gave us a reproducible code sample of your problem, that is a piece of code that, when we run it, produces the error you get.

numbers should be part of the instance, bad to use a global

Here is my code:

#!/usr/bin/python2

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label

numbers = []

class CalculatorApp(App):
    def build(self):

        b = BoxLayout(orientation = "vertical")
        self.l = Label(text = " ")
        g = GridLayout(rows = 3)
        btn1 = Button(text = "1")
        btn2 = Button(text = "2")
        btn3 = Button(text = "3")
        btn4 = Button(text = "4")
        btn5 = Button(text = "5")
        btn6 = Button(text = "6")
        btn7 = Button(text = "7")
        btn8 = Button(text = "8")
        btn9 = Button(text = "9")
        btn10 = Button(text = "0")
        btn11 = Button(text = "+")
        btn12 = Button(text = "-")
        btn13 = Button(text = "*")
        btn14 = Button(text = "/")
        btn15 = Button(text = "=")
        btn16 = Button(text = "clear")
        btn17 = Button(text = "(")
        btn18 = Button(text = ")")

        btn1.bind(on_press = self.number_one)
        btn2.bind(on_press = self.number_two)
        btn3.bind(on_press = self.number_three)
        btn4.bind(on_press = self.number_four)
        btn5.bind(on_press = self.number_five)
        btn6.bind(on_press = self.number_six)
        btn7.bind(on_press = self.number_seven)
        btn8.bind(on_press = self.number_eight)
        btn9.bind(on_press = self.number_nine)
        btn10.bind(on_press = self.number_zero)
        btn11.bind(on_press = self.set_operator_to_add)
        btn12.bind(on_press = self.set_operator_to_sub)
        btn13.bind(on_press = self.set_operator_to_multy)
        btn14.bind(on_press = self.set_operator_to_divid)
        btn15.bind(on_press = self.answer)

        g.add_widget(btn1)
        g.add_widget(btn2)
        g.add_widget(btn3)
        g.add_widget(btn4)
        g.add_widget(btn5)
        g.add_widget(btn6)
        g.add_widget(btn7)
        g.add_widget(btn8)
        g.add_widget(btn9)
        g.add_widget(btn10)
        g.add_widget(btn11)
        g.add_widget(btn12)
        g.add_widget(btn13)
        g.add_widget(btn14)
        g.add_widget(btn15)

        b.add_widget(self.l)
        b.add_widget(g)

        return b

    def check(self):
        global numbers
        if len(numbers) == 3:
            self.answer()

    def answer(self, event):
        result = eval(" ".join(str(num) for num in numbers))
        print result
        self.l.text = str(result)
        numbers[:] = []
        numbers.append(result)


    def number_one(self, event):
        global numbers
        numbers.append(1)
        print numbers
        self.check()

    def number_two(self, event):
        global numbers
        numbers.append(2)
        print numbers
        self.check()

    def number_three(self, event):
        global numbers
        numbers.append(3)
        print numbers
        self.check()

    def number_four(self, event):
        global numbers
        numbers.append(4)
        print numbers
        self.check()

    def number_five(self, event):
        global numbers
        numbers.append(5)
        print numbers
        self.check()

    def number_six(self, event):
        global numbers
        numbers.append(6)
        print numbers
        self.check()

    def number_seven(self, event):
        global numbers
        numbers.append(7)
        print numbers
        self.check()

    def number_eight(self, event):
        global numbers
        numbers.append(8)
        print numbers
        self.check()

    def number_nine(self, event):
        global numbers
        numbers.append(9)
        print numbers
        self.check()

    def number_zero(self, event):
        global numbers
        numbers.append(0)
        print numbers
        self.check()



    def set_operator_to_add(self, event):
        global numbers
        numbers.append("+")
        print numbers
        self.check()

    def set_operator_to_sub(self, event):
        global numbers
        numbers.append("-")
        print numbers
        self.check()

    def set_operator_to_multy(self, event):
        global numbers
        numbers.append("*")
        print numbers
        self.check()

    def set_operator_to_divid(self, event):
        global numbers
        numbers.append("/")
        print numbers
        self.check()

if __name__ == "__main__":
    CalculatorApp().run()

There was a def check_to_answer(): that i changed into def answer():.

I don't get the error with check(), besides you really don't need it.
You may have something like 123+45 to solve.
My (still brute force code) suggestion ...

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label

#numbers = []

class CalculatorApp(App):
    def build(self):

        self.numbers = []
        b = BoxLayout(orientation = "vertical")
        self.l = Label(text = " ")
        g = GridLayout(rows = 3)
        btn1 = Button(text = "1")
        btn2 = Button(text = "2")
        btn3 = Button(text = "3")
        btn4 = Button(text = "4")
        btn5 = Button(text = "5")
        btn6 = Button(text = "6")
        btn7 = Button(text = "7")
        btn8 = Button(text = "8")
        btn9 = Button(text = "9")
        btn10 = Button(text = "0")
        btn11 = Button(text = "+")
        btn12 = Button(text = "-")
        btn13 = Button(text = "*")
        btn14 = Button(text = "/")
        btn15 = Button(text = "=")
        btn16 = Button(text = "clear")
        btn17 = Button(text = "(")
        btn18 = Button(text = ")")

        btn1.bind(on_press = self.number_one)
        btn2.bind(on_press = self.number_two)
        btn3.bind(on_press = self.number_three)
        btn4.bind(on_press = self.number_four)
        btn5.bind(on_press = self.number_five)
        btn6.bind(on_press = self.number_six)
        btn7.bind(on_press = self.number_seven)
        btn8.bind(on_press = self.number_eight)
        btn9.bind(on_press = self.number_nine)
        btn10.bind(on_press = self.number_zero)
        btn11.bind(on_press = self.set_operator_to_add)
        btn12.bind(on_press = self.set_operator_to_sub)
        btn13.bind(on_press = self.set_operator_to_multy)
        btn14.bind(on_press = self.set_operator_to_divid)
        btn15.bind(on_press = self.answer)

        g.add_widget(btn1)
        g.add_widget(btn2)
        g.add_widget(btn3)
        g.add_widget(btn4)
        g.add_widget(btn5)
        g.add_widget(btn6)
        g.add_widget(btn7)
        g.add_widget(btn8)
        g.add_widget(btn9)
        g.add_widget(btn10)
        g.add_widget(btn11)
        g.add_widget(btn12)
        g.add_widget(btn13)
        g.add_widget(btn14)
        g.add_widget(btn15)

        b.add_widget(self.l)
        b.add_widget(g)

        return b

    def answer(self, event=None):
        result = eval("".join(str(num) for num in self.numbers))  # " " --> ""
        print(result)
        print("".join(str(num) for num in self.numbers))  # test
        self.l.text = str(result)
        self.numbers = []

    def number_one(self, event):
        self.numbers.append(1)
        print(self.numbers)

    def number_two(self, event):
        self.numbers.append(2)
        print(self.numbers)

    def number_three(self, event):
        self.numbers.append(3)
        print(self.numbers)

    def number_four(self, event):
        self.numbers.append(4)
        print(self.numbers)

    def number_five(self, event):
        self.numbers.append(5)
        print(self.numbers)

    def number_six(self, event):
        self.numbers.append(6)
        print(self.numbers)

    def number_seven(self, event):
        self.numbers.append(7)
        print(self.numbers)

    def number_eight(self, event):
        self.numbers.append(8)
        print(self.numbers)

    def number_nine(self, event):
        self.numbers.append(9)
        print(self.numbers)

    def number_zero(self, event):
        self.numbers.append(0)
        print(self.numbers)

    def set_operator_to_add(self, event):
        self.numbers.append("+")
        print(self.numbers)

    def set_operator_to_sub(self, event):
        self.numbers.append("-")
        print(self.numbers)

    def set_operator_to_multy(self, event):
        self.numbers.append("*")
        print(self.numbers)

    def set_operator_to_divid(self, event):
        self.numbers.append("/")
        print(self.numbers)

if __name__ == "__main__":
    CalculatorApp().run()

The code cries out for loops!

@vegaseat you didn't get the error with check() because as i said, there was a def check_to_answer(): that i changed into def answer():, then i got some errors again, so i sent the complete code file.

Well, with your edited code, there is a problem.
for example if i want to add three numbers with eachother, for exampleif i press 2 then + then 3 then * then 4, all of them will be appended into a list and the result will be number 14, because the calculator will first attention to 3x4 because of the * sign and then will add 12 to number 2 and will show us number 14 as the result!

But i wanted my code to do like this:
To create a list that takes only three indexes, a first number then a sign then the second number, and check the list after every appending, to see the length of the list, if it is three, run the operation and then show the result as a label and then empty the list and append the result into the list. So if i enter 2 then + then 3, the function understand that the length of the list is three so it will calculate 2+3 ans show number 5 as the label and then empty the list and finally append number 5 into list.
Then if i press * and then 4, the function understand the length of the list is again three, (5x4), so it will calculate it and will show number 20 as the result for the label and i can continue like that.

And with the button and functin clear(), i can empty the list and make it ready to take a new number and go on.

So can you help me with my code with solving it's errors? Can you run my code again and tell me what is the problem and how can i solve it please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.