I’m playing around with Kivy a bit and can’t understand why MenuScreen.number_list doesn’t return the list from the MenuScreen class.
class MenuScreen(Screen): def process(self): self.text = self.ids.input.text print(self.text) pass def submit(self): number_list = [] for i in self.text: number_list.append(int(i)) class GuessScreen(Screen): def guess(self, number): for i in MenuScreen.number_list: if correct == True: if number == i: print("Correct, next number") correct = True else: print("Wrong") correct = False pass
Advertisement
Answer
You need to declare number_list
as a class property so you can use it, otherwise it’s just a variable with its scope only inside the method.
class MenuScreen(Screen): number_list = [] def process(self): self.text = self.ids.input.text print(self.text) pass def submit(self): number_list = [] for i in self.text: number_list.append(int(i)) class GuessScreen(Screen): def guess(self, number): MenuScreen = MenuScreen(Screen) MenuScreen.submit() for i in MenuScreen.number_list: if correct == True: if number == i: print("Correct, next number") correct = True else: print("Wrong") correct = False pass
And also make sure you create an object for the class and call its methods