Help, list index out of range

18 views
Skip to first unread message

Claver Barreto

unread,
Jul 25, 2019, 5:58:50 AM7/25/19
to Kivy users support
so, i need help trying to figure out why this error keep appearing to me , heres my

python code:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import random

class MainScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class GameScreen2(Screen):
    pass

class GameScreen3(Screen):
    pass

class GameScreen4(Screen):
    pass

class About(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("quoraedit.kv")

class MainApp(App):
   
    def build(self):
   
        self.load_sounds()

        return presentation
   
    def load_sounds(self):
        self.sounds = {}
        for i in range(10):
            fname = 'sound' + str(i+1) + '.wav'
            self.sounds[i] = SoundLoader.load(fname)
   
    def play_sound1(self):
        sound = self.sounds.get(0)
        if sound is not None:
            sound.volume = 0.5
            sound.play()
           
    def play_sound2(self):
        sound = self.sounds.get(1)
        if sound is not None:
            sound.volume = 0.5
            sound.play()
           

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

but i dont think the error comes from the python code.

HERES THE

KV CODE:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import Image kivy.uix.image.Image
#: import SoundLoader kivy.core.audio.SoundLoader
#: import random random


ScreenManagement:
   
    transition: FadeTransition()
   
    MainScreen:
   
    GameScreen:
   
    GameScreen2:
   
    GameScreen3:
   
    GameScreen4:
   
    About:

<Label>:

    font_size: 40
    color: 0,1,0,1
    pos_hint: {"x":0, "y": 0}

<Button>:
   
    font_size: 14
    size_hint: 0.2, 0.1
    color: 0,1,0,1
<MainScreen>:
   
   
    name: str(0)
   
    FloatLayout:
               
    Button:
        text: "START GAME"
        pos_hint: {"x": 0, "y":0}
   
        on_release:
            screens = [1, 2, 3, 4]
            root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]
   
    Button:
        text: "About"
        pos_hint: {"x": .8, "y":0}
        on_release: root.manager.current = str(5)
   

<GameScreen>:
    name: str(1)
   
    FloatLayout:
        Label:
            text: "Python\nSnowden\nMr.Robot"
   
    Button:
        text: "Next"
        on_release:
            screens = [1,2,3,4]
            root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]


<GameScreen2>:
    name: str(2)
       
    FloatLayout:
       
        Label:
            text: "Banana\n\nOrange\n\nTea\n\nSleep"
           
    Button:       
        text: "Next"
        on_release:
            screens = [1,2,3,4]
            root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]
           
<GameScreen3>:
    name: str(3)
   
    FloatLayout:
   
        Label:
            text: "Assembly\n\nRuby\n\nC"
       
    Button:   
        text: "Next"
        on_release:
            screens = [1,2,3,4]
            root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]
           
<GameScreen4>:
    name: str(4)
   
    FloatLayout:
   
        Label:
            text: "Prolog\n\nPygame\n\nC++"
           
    Button:       
        text: "Next"
        on_release:
            screens = [1,2,3,4]
            root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]

<About>:
    name: str(5)
   
    FloatLayout:
        Label:
            text: "About Screen"
           
    Button:   
        text: "Home"
        on_release: app.root.current = str(0)

so i set every screen name to 1,2,3,4 except for the menu screen which is 0 and the about screen which is 5 so i wanted to switch screens randomly except the menu screen and the About screen these two must be isolated, out of the "random screen switching loop" so in the buttons for the switch i made a list of numbers(refering to each screen name) but i didnt put the menu or the about screen names the(0 and 5) so it wouldnt be picked up. i used randint first and after some switches an "index out of range" error came up, so i used random.choice and the error came again.

COULD SOMEONE PLEASE HELP ME?
ive attached the python and kv files.

quora.py
quoraedit.kv

Elliot Garbus

unread,
Jul 25, 2019, 9:25:42 AM7/25/19
to kivy-...@googlegroups.com

Change:

root.manager.current = root.manager.screen_names[screens[random.choice(screens)]]

 

To:

root.manager.current = root.manager.screen_names[random.choice(screens)]

 

You don’t need that second level of indirection.  The call to random.choice is selecting one of the possible screens.

You may want to consider taking the current screen out of the list of possible choices.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ff5977ae-ee08-4355-88d4-2843239d15b0%40googlegroups.com.

 

Claver Barreto

unread,
Jul 25, 2019, 11:51:46 AM7/25/19
to Kivy users support
placed you code and ive been clicking for like 40min now and its working perfectly, thank you.

Reply all
Reply to author
Forward
0 new messages