How to update player score and computer score without constantly changing it to zero

How do I update the player and computer score without it constantly changing to zero?
I want the game to continue in a loop while saving the score of the computer and player in each round

In your player_input function, replace:

computerscore = 0
playerscore = 0


global computerscore
global playerscore
1 Like

I would highly avoid doing this if possible, and IIRC it will cause an error unless you keep the original two lines along with the global declaration. A simple way to refactor would be to move

computerscore = 0
playerscore = 0

out of the player_input() function.


i get syntax errors when i do that

The variables are local variables and so there would be an error

Could you copy and post the full error message please?


i = 0
def f():
  i += 1

Gives an error because by default you cannot set global variables.
So you have to put global i at the start of f.


Oh I guess your right, sorry about that

I swear this worked before :laughing:

And add computerscore n playerscore as parameters to the player_input() function.


1 Like

I’d recommend trying what @QwertyQwerty88 said


i just tried that, it still keeps reseting the player score back to zero

A recommendation I would do is to avoid using Global Variables and start to using classes instead.

You’re passing playerscore and computerscore as arguments to the player_input function, you’re not updating the global variables after the function call.

def player_input(myinput, playerscore, computerscore):
    computer = random.choice(computer_choice)

    #rest of your code

    # At the end of the function, return the updated scores
    return (result, playerscore, computerscore)

And change here too:

while True:
    myinput = input('Player input: ')
    result, playerscore, computerscore = player_input(myinput, playerscore, computerscore) #add this line
1 Like

could you explain what the result variable is in your comment?

i am also abit confused on this part of your code

In your original code, the player_input function only returned a string message indicating the result of the round.
In my code, the function returns three values: the result message, the updated playerscore and the update computerscore.

This is done by using the line return (result, playerscore, computerscore). In python, you can return multiple values from a function, and they are returned as a tuple.

In the while loop, when the player_input function is called, the returned values are unpacked into the last three variables.
This means that after each round, the global playerscore/computerscore are updated with the values returned from the function.

The main change is the way the scores are managed. Instead of trying to modify global variables from within the function (which can lead to errors), the function now returns the updated scores.

1 Like

Using global variables is generally bad, but without classes it is often the simplest solution.
Another approach is to pass in a mutable “game state” object to player_input, which can be either a dict or custom class. This way, you don’t have to return anything.

I implemented your lines of code and the game seems to work for while. For some reason however, the scores become inconsistent sometimes computer score increases by 1 or returns to zero

I also tried making the variables inside my function global and it does the samething