0

This is a simplified version I made of the problem I'm having

I have my Game() function that calls the other UpdateScore() and Score() functions, UpdateScore() adds 1 to computerScore, and Score() prints that updated value. But that value either doesn't get updated, what am I doing wrong?

import time 

def Game():
    condition = True
    computerScore = 0
    while (condition):
        time.sleep(2)
        UpdateScore(computerScore)
        Score(computerScore)
    
def UpdateScore(computerScore):
    computerScore = computerScore+1
    return
    
def Score(computerScore):
    computerScore = UpdateScore(computerScore)
    print(computerScore)
    
Game()
  • [This answer](https://stackoverflow.com/a/986145/2081835) should help. In Python, some types are mutable; others aren't. `Int` is not a mutable type, so the value you're updating is a new value. – theherk Oct 22 '22 at 08:39

4 Answers4

1

Python doesn't pass variables by value. It doesn't pass it by reference either. It passes variables by assignment.

Refer to the code snippet below.

>>> def main():
...     n = 9001
...     print(f"Initial address of n: {id(n)}")
...     increment(n)
...     print(f"Final address of n: {id(n)}")
...
>>> def increment(x):
...     print(f"Initial address of x: {id(x)}")
...     x += 1
...     print(f"Final address of x: {id(x)}")
...
>>> main()
Initial address of n: 140562586057840
Initial address of x: 140562586057840
Final address of x: 140562586057968
Final address of n: 140562586057840

You can notice how the address remains the same after passing to the function but only changes after the increment.

There is a simple fix to this. You can simply return computerScore from the updateScore() function and assign it to the original variable in the Game() function. Check the modified code below.

import time 

def Game():
    condition = True
    computerScore = 0
    while (condition):
        time.sleep(2)
        computerScore = UpdateScore(computerScore)
        Score(computerScore)
    
def UpdateScore(computerScore):
    computerScore = computerScore+1
    return computerScore
    
def Score(computerScore):
    computerScore = UpdateScore(computerScore)
    print(computerScore)
    
Game()

Cheers.

0

UpdateScore is not returning the updated value. Add a return statement there

The below code gives an output of 1,2


import time 

def Game():
    condition = True
    computerScore = 0
    while (computerScore < 2): #Change condition to avoid infinite loop
        computerScore = UpdateScore(computerScore)
        Score(computerScore)
    
def UpdateScore(computerScore):
    computerScore = computerScore+1
    return computerScore
    
def Score(computerScore): # Score is just printing the o/p
    print(computerScore)
    

Game()
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
0

"computerScore" is a value thats being passed around, so any changes that you make to it in subsequent function calls will not affect the original value, you will have to return the updated value for the change to be reflected, which in your case is not being done. A working sample can look something like below.

import time 

def Game():
    condition = True
    computerScore = 0
    while (condition):
        time.sleep(2)
        computerScore = UpdateScore(computerScore)
        Score(computerScore)
    
def UpdateScore(computerScore):
    computerScore = computerScore+1
    return computerScore
    
def Score(computerScore):
    print(computerScore)
    
Game()
0

I am not sure what you are trying to do here.

So, when you call the UpdateScore function, you are creating & updating a new variable, that's why your original one does not get updated. Why not just put computerScore=computerScore+1 (computerScore += 1 also works) in your original code instead of creating a new function? Also, you can just print() at the end with no extra function.

You might want to use a class in that particular case.

I hope this helps.