lundi 26 septembre 2022

Random questions without repetition python kivy

I am on my way to build a quiz app, the questions and answer options are in a image displayed in a screen. Each question have theyr own screen, everything works but what i try to do now is to call those screens randomly and this is working too but what i want to do now is to not let the app to display the same question(screen) again and also everytime when i start the app is starting with the same screen. I tried to do something with np.random.permutation but with no succes.. i need to mention that i am new in python / kivy. Is somebody here to help me to pass this step ?? THANKS FOR YOUR HELP !!

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
import random
import numpy as np


class Q1(Screen):
    pass


class Q2(Screen):
    pass


class Q3(Screen):
    pass


class Q4(Screen):
    pass


class WrongAnswer(Screen):
    pass


class TestApp(App):

    def wrong_answer(self):
        screen = ['WrongAnswer']
        return random.choice(screen)

    def random_screen(self):
        screens = list(np.random.permutation(np.arange(0,4))['Q1', 'Q2', 'Q3', 'Q4'])
        return random.choice(screens)

    def build(self):

        sm = ScreenManager()
        sm.add_widget(Q1(name='Q1'))
        sm.add_widget(Q2(name='Q2'))
        sm.add_widget(Q3(name='Q3'))
        sm.add_widget(Q4(name='Q4'))
        sm.add_widget(WrongAnswer(name='WrongAnswer'))

        return sm


if __name__ == '__main__':
    TestApp().run()

<Q1>:
    Image:
        source: 'Q1.png'

        FloatLayout:
            size: root.width, root.height/2

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.random_screen()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()


<Q2>:
    Image:
        source: 'Q2.png'

        FloatLayout:
            size: root.width, root.height/2

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.random_screen()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

<Q3>:
    Image:
        source: 'Q3.png'

        FloatLayout:
            size: root.width, root.height/2

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.random_screen()

<Q4>:
    Image:
        source: 'Q4.png'

        FloatLayout:
            size: root.width, root.height/2

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.random_screen()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":1.16}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.5, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

            Button:
                size_hint: 0.3, 0.25
                pos_hint: {"x":0.09, "top":0.7}
                background_color: 1, 1, 1, 0.2
                on_release: root.manager.current = app.wrong_answer()

<WrongAnswer>:
    Image:
        source: 'L1.png'



Aucun commentaire:

Enregistrer un commentaire