A little help on a project

I am making a rock paper scissors game and I believe I have most of it working. However, I cannot figure out how to make the program stop when the score reaches a certain point. I think the main problem may be in lines 22-34.

[https://replit.com/@DawsonMartin2/Rock-Paper-Scissors]

while 1 == 1:
    game_logic()
    if user_score == winning_score or computer_score == winning_score:
      break
  
if user_score == winning_score:
    print('''
You win!''')

if computer_score == winning_score:
    print('''
You lose :(''')

I have also tried setting the break/if statement as the parameters of the while loop. I am fairly new at python/coding in general so if you have any other tips, please let me know. Thank you so much for any help!

Hey @DawsonMartin2, welcome to the community!

The reason why your code is still running is because your break keyword breaks out of your if statement, not your while loop.

To do this, just change the while loop to stop if user_score or computer_score is equals to winning_score

while (user_score != winning_score) and (computer != winning_score):
    game_logic()

if user_score == winning_score:
    print('''
You win!''')

if computer_score == winning_score:
    print('''
You lose :(''')

If this helped you, please mark this as the solution so that others who have the same problem can easily fix their issue. Thanks!

1 Like

You- You can’t break out of an if statement…

1 Like

The code should still work nonetheless.

After a bit of digging, I found the issue. It has to do with the way imported variables are stored and updated. What I would guess is happening, is that the variable is just a number, so it is stored as a “constant”, which means the only way to change it is to completely reassign the value. So when you change the variable in game_logic.py it replaces the old one with a new number and the one used in main.py stays the same. (that wasn’t a very good explanation but I’m not sure how to explain it really)

Example of the problem:


main.py

from otherfile import my_var, changeMyVar, getMyVar

print(my_var, getMyVar())
changeMyVar(7)
print(my_var, getMyVar())

otherfile.py

my_var = 10

def changeMyVar(x):
    global my_var
    my_var = x

def getMyVar():
    return my_var

Now if you run main.py, the output should look like this.

10 10
10 7

You can see that the variable used in main.py doesn’t update, and therefore has the wrong value.

Solution


To solve this problem, just create a getter function inside of game_logic.py that returns the scores as they should be.

add to game_logic.py

def get_scores():
    return user_score, computer_score

use in main.py

user_score, computer_score = get_scores()

One other thing I would recommend is instead of doing from game_logic import *, only import what you will actually use, like this from game_logic import game_logic, get_scores and then inside of your loop you call this to get the scores user_score, computer_score = get_scores().

There are other ways to do it, but if the scores are just global variables that are just numbers then this is how you should do it.

3 Likes

My suggestion is to move away from global variables as they are a bad habit to have.
It is great to use them as constants, but try to avoid them.

I would change your game_logic()into something can can be used in this matter:

user_score, computer_score = game_logic(user_score, computer_score)

This might seems tedious, but it is better programming as it isolates parts of your code as much as possible.

Or even consider working with classes (that could be your next game version) that would truly make the game code more elegant.

3 Likes

Thanks for the help, I tried to put in the getter function and change the way I import but it didn’t seem to work. I may be calling get_scores() in the wrong place. Can you try to explain please?

Thank you for helping. I’m not really sure the difference between a global variable and whatever isn’t a global variable. Also, is the function you suggested the same as the “getter” function that SharkCoding suggested, just as part of the game_logic() and not separate?

First of all, you shouldn’t need this global user_score, computer_score at the top of the file. And also, you need the code before the while loop like this.

user_score, computer_score = get_scores()
while (user_score != winning_score) and (computer_score != winning_score):
    game_logic()

However, similar to what @whileTRUEpass said, it would be better to make your function return the string “win”, “lose”, or “tied” and then update the scores in main.py so you only have the score variables in one file.

2 Likes

Thank you so much for your help, I was finally able to get it to work. I may still do a little bit of cleanup and add a timer, but I am really happy now!

1 Like

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