Home > database >  kivy endless while loop
kivy endless while loop

Time:11-20

i'm a new begginer to python and kivy, and recently i was trying to make a learning application to work and i don't know how to make it running without stoping ? this is my code

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from random import randrange
import time


#verbs and answers
verbs = ["arise", "awake", "be", "bear", "beat", "become", "begin", "bend", "bet", "bind", "bite", "bleed", "blow", "break", "breed", "bring", "broadcast", "build", "burn", "burst"]
answers =["arose", "awoke", "was", "bore", "beat", "became", "began", "bent", "bet", "bound", "bit", "bled", "blew", "broke", "bred", "brought", "broadcast", "built", "burnt", "burst"]
range = 20


    


rand = randrange(0, range)




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


        


        self.window = GridLayout()
        self.window.cols = 1
            

        self.window.add_widget(Image(source='cat.png'))

        self.greeting = Label(text=verbs[rand])
        self.window.add_widget(self.greeting)




        self.answer = TextInput(multiline=False)
        self.window.add_widget(self.answer)

        self.button = Button(text="check")
        self.button.bind(on_press=self.check_answer)
        
        self.window.add_widget(self.button)
        return self.window
    def check_answer(self, instance):

        if self.answer.text == answers[rand]:
            self.greeting.text = 'yes!'   self.answer.text   ' is the correct verb'

        else:
            self.greeting.text = "no!"


        

        


        

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

the app is asking for a verb and then check it if it is correct or not but it is working one time. how can i add while loop to make it asking for a answer every time and checking if it is correct or not please ? enter image description here

CodePudding user response:

It's already sticking around forever, right? The only issue is that it only chooses one word, and sticks with that word forever. So, when they guess correctly, choose another word:

    def check_answer(self, instance):
        global rand
        if self.answer.text == answers[rand]:
            rand = randrange(0,range)
            self.greeting.text = 'yes!'   self.answer.text   ' is the correct verb\n\nNow try: "   verb[rand]
        else:
            self.greeting.text = "no!"

Now, rather than use a global for this, it would be better to store this in a class variable:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from random import randrange
import time


#verbs and answers
verbs = ["arise", "awake", "be", "bear", "beat", "become", "begin", "bend", "bet", "bind", "bite", "bleed", "blow", "break", "breed", "bring", "broadcast", "build", "burn", "burst"]
answers =["arose", "awoke", "was", "bore", "beat", "became", "began", "bent", "bet", "bound", "bit", "bled", "blew", "broke", "bred", "brought", "broadcast", "built", "burnt", "burst"]
range = 20



class english_learning(App):
    def build(self):
        self.target = randrange(0, range)

        self.window = GridLayout()
        self.window.cols = 1
        self.window.add_widget(Image(source='cat.png'))

        self.greeting = Label(text=verbs[self.target])
        self.window.add_widget(self.greeting)

        self.answer = TextInput(multiline=False)
        self.window.add_widget(self.answer)

        self.button = Button(text="check")
        self.button.bind(on_press=self.check_answer)
        
        self.window.add_widget(self.button)
        return self.window

    def check_answer(self, instance):

        if self.answer.text == answers[self.target]:
            self.target = randrange(0, range)
            self.greeting.text = 'yes!'   self.answer.text   ' is the correct verb\n\nNow try:'   verbs[self.target]
        else:
            self.greeting.text = "no!"

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

Really, you probably want a separate line for the "verb" and the "greeting".

  • Related