How can I fix my loop?

Question:
I am trying to make a while loop and I can’t get it to loop again. I was hoping to make it so the loop generates a random “set” of shapes with a built in onMousePress if statement to decide if the user is correct. after the user clicks on a shape it will return correct and incorrect “points” and start the loop again to generate another set. I cannot get this to work, as I don’t really know how to get the loop to start again. I tried a couple different things, but I am not very experienced, so I’m sure there’s a better way that I am missing. Any help would be appreciated on how to fix this, thanks.

context:
The goal of the game is to select the shape with a color that doesn’t match. The correct shape for each set has a variable assigned to it so onMousePress can check if it is within the correct shape.

``````while score < 15 and wrong < 3:
sets = random.choice(groups)
if sets == 1:
#covers previous objects
Rect(0,0,500,500,fill = "white")
#
Rect(0,0,90,150,fill=rgb(228, 55, 237))
Rect(100,0,90,150,fill=rgb(228, 55, 237))
Rect(200,0,90,150,fill=rgb(228, 55, 237))
Rect(300,0,90,150,fill=rgb(228, 55, 237))
Rect(0,155,90,150,fill=rgb(228, 55, 237))
Rect(100,155,90,150,fill=rgb(228, 55, 237))
c_1 = Rect(200,155,90,150,fill=rgb(151, 59, 156)) #correct
Rect(300,155,90,150,fill=rgb(228, 55, 237))
def onMousePress(mouseX,mouseY):
if c_1.contains(mouseX,mouseY):
print("correct!")
return score + 1

else:
print("wrong")
return wrong + 1
``````

Hello, it looks like you are trying to change the variable `score` or `wrong` when the user clicks an object.
I’m assuming that the `onMousePress` is indeed called as intended, when the user clicks the correct shape.
To change a variable outside of the function that is trying to change it, you must do some extra things. First of all, I don’t see how returning the change will affect anything at all.
So, you would increment the variable like so:

``````score += 1
``````

However, you must also tell python that `score` is actually a global variable, that means it is defined outside of any functions (it’s not a local, function variable). So, put this at the start of the `onMousePress` function (or anywhere before setting the variable:

``global score, wrong``
1 Like

Thank you for the advice, I have changed the things you mentioned. Though I’m still not sure how to make the loop start again after a selection is made. Would you be able to advise me on how that can be fixed?

I think your loop would start again if you removed the `break` at the end of the loop.
Also, you must put `global score, wrong` inside the `onMousePress` function.
Also, it should be like `score += 1` instead of `score =+ 1` when trying to increment a variable.
You shouldn’t call `gameLoop()` from inside `onMousePress`, not because it’ll error, but because it’s a bad practice.

Oh thank you that is good to know. Removing the break caused an error message saying it generated 2000 shapes. I assume it ran infinitely and replit stopped it. I also removed the gameLoop() as it did not call the loop to run again like I had hoped, and I removed it. Is there a better way to make the loop run again after the onMousePress function? Thank you for all your help btw, I am learning a lot.

So, the problem is now this. You need the loop to wait for the user to click, otherwise it will generate a ton of shapes. But you don’t really need the loop for it.
I think the best way to do this is to separate the drawing shapes from the game loop altogether. Instead, put it inside the `onMousePress` function (no loop). Of course, call it once at the start of the program.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.