Python Shell cannot run a module with more than one while loop

Asked

Viewed 164 times

1

I have serious problems in being able to build a functional module in which I have to use more than 1 while loop, because the program stops running.

Especially when I use modules like pygame and Tkinter, where my goal is to build a game, but I need to use more than 1 while loop, only if I run it stops working. In this situation I intend to run two while loops at the same time, but the window fails to respond. Why?

----------------------------#######--------------------------------------

Also I did not understand why my while loop does not cease to exist when I turn the variable 'playing' True? In this situation I just want to run a while loop at a time, the switch off of the while loop is when the 'playing' variable becomes True. Here’s the code:

playing = False
def play():
    playing = True

def gameloop():
    ...
    play()

while not playing:
    gameloop()

while playing:
    gameDisplay.fill(black)
    pygame.display.update()
    clock.tick(15)

The problem is that the second while loop does not run and the first one does not stop running when I call the play() function, even if it declares the global 'playing' variable. Why?

Using python 3.5.0 with Windows 10

I see no reason for windows not responding when I run more than one while loop, because with other people’s pc works perfectly. The problem will be from my pc?

  • What do you mean, "it stops working"? Describe in more detail the observed behavior.

  • Do you want to leave the two loops running together? Or the while below should only be started when playing for true? Describe it better as @Pabloalmeida says.

  • @Pabloalmeida how I am working with Tkinter the window stops working('does not answer')

  • @Christianfelipe Yes I want both loops at the same time, but only in the second situation. And the bass while should only be started when playing for True

  • But in your logic, if the playing for True the top while ends, so you’ll only have one while running anyway.

  • @Christianfelipe because when I run the program and turn the variable 'playing' True o while from above does not stop running and while from below does not start running

Show 1 more comment

1 answer

2

The run that when you put:

def play():
    playing = True

The interpreter created playing as a local variable, so even if you change the value within play the playing global will remain False.

Declaration of the global variable

To tell the interpreter that you want to use the global variable, pass the attribute global for the variable in the method. If the variable does not exist, it is instantiated. Tested in python3.4 (Gentoo Linux)

def play():
    global playing
    playing = True

Contrary to what many beginners think, to use a global variable it is not enough to just declare it at the top of the file, you need to specify in the method that will use a global variable.

If it does not work, it is possible to use alternative methods.

Library

vars.py:

playing = False

main py.:

import vars

...

def play():
    playing = True

...

while not vars.playing:
    ...

This method, imports the variables of a python file, so when it references the variable, it is passed its path so the method does not create a local variable.

Static

def gameloop():
    self.playing = True
gameloop.playing = False

...

while not gameloop.playing:

Dict

conf = {
    'playing' : False,
}

def play():
    conf['playing'] = True

...

while not conf['playing']:
    ...
  • But to make the global variable, I just need to use the global attribute and declare the global variable, right? What I’ve tried to do but it doesn’t work the same

  • @Inêsbaratafeioborges, I added an excerpt with the declaration of the global variable within play. The test worked on my computer, but if the error persists, try running the code on another version of python or on a virtual machine with another OS. sometimes there may be some error in the interpreter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.