-1

I saved my variables at the start of my program and allow the functions to access them I believe but when they run the value is not saved when the function repeats.

P1_score = 0
P2_score = 0
round_number = 0

def dice_rolling():
    # P1D1 means player ones dice one value and so on with P1D2
    import random
    # player ones turn
    print("player ones turn")
    P1D1 = random.randint(1, 6)
    print("your number is ", P1D1)
    P1D2 = random.randint(1, 6)
    print("your second number is", P1D2)
    # player twos turn
    print("player twos turn")
    P2D1 = random.randint(1, 6)
    print("your number is", P2D1)
    P2D2 = random.randint(1, 6)
    print("your second number is", P2D2)
    score_calculation(P1D1, P1D2, P2D1, P2D2,P1_score,P2_score,round_number)


def score_calculation(P1D1, P1D2, P2D1, P2D2,P1_score,P2_score,round_number):
    import random
    
    round_number = round_number + 1
    # player 1 score calculation
    total_P1 = P1D1 + P1D2
    P1_score = P1_score + total_P1
    if total_P1 % 2 == 0:
        P1_score = P1_score + 10
    else:
        P1_score = P1_score + 5
    if P1D1 == P1D2:
        P1D3 = random.randint(1, 6)
        P1_score = P1_score + P1D3

    # player 2 score calculation
    total_P2 = P2D1 + P2D2
    P2_score = P2_score + total_P2
    if total_P2 % 2 == 0:
        P2_score = P2_score + 10
    else:
        P2_score = P2_score + 5
    if P2D1 == P2D2:
        P2D3 = random.randint(1, 6)
        P2_score = P2_score + P2D3
    print("player ones score at the end of round", round_number, "is", P1_score)
    print("player twos score at the end of round",round_number,"is",P2_score)

    
for x in range(0,5):
    dice_rolling() 

Any help would be appreciated and if someone could give a simple explanation as to what I'm doing wrong and what to fix would be great.

iknow
  • 8,358
  • 12
  • 41
  • 68
drewpie
  • 21
  • 1
  • 7
  • local variables inside a function do not reflect changes in outer scopes. Please consider reading [the scope docs](https://docs.python.org/3/tutorial/classes.html#scopes-and-namespaces-example). If you want to access a global variable within a function, use the [`global`](https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python) keyword. – Chase Jul 22 '20 at 10:32
  • will have a read thank you – drewpie Jul 22 '20 at 10:33
  • Also, please explain *which* variables you think are not reflecting changes as well as how you expect the variables to behave. Your question is very unclear right now. – Chase Jul 22 '20 at 10:35
  • As a programmer you should try very, *very*, **very** hard not to use globals. – quamrana Jul 22 '20 at 10:37
  • how would i turn these into locals by any chance – drewpie Jul 22 '20 at 10:41

2 Answers2

0

Python can read from global variables inside a function, but can't assign them without some extra work. In general, when you want to use a global variable, it's a good idea to make it explicit by using the global keyword in your function:

my_global_var = 0

def some_function():
    global my_gobal_var
    
    my_global_var = 10

print(my_global_var) # it prints 10

somefunction() # modifies the global var

print(my_global_var) # now it prints 10
Willem Meints
  • 1,152
  • 14
  • 33
0

Variables are defined and used locally. Consider this example.

x = 1 #initial value
def test(x):
    print(x) #print what you got
    x += 1
    print(x) #print updated value

print(x) #Actual printouts here
test(x)
print(x)

This results in :

1 
1 #value when entering the function
2 #Value changed by function
1 #value outside the function did not change

If you want the variables to be maintained in functions, consider using class variables or global variables. (I recommend avoiding globals as you get to more complex problems)

Global example:

global x
x = 1
def test():
    global x
    x+=1
print(x)
test()
print(x)
test()
print(x)

Results in :

1
2
3

Finally class variables:

class test_class():
    def __init__(self):
        self.x = 1 #variables defined like this are class variables
    def test(self):
        self.x += 1 #Advantages of class variables is that you can defined them in the __init__ function
    def running(self):
        print(self.x) # And you can access it from multiple functions without using globals and much more too. 
        self.test()
        print(self.x)


if __name__ == '__main__':
    tclass = test_class()
    tclass.running()
Jason Chia
  • 1,144
  • 1
  • 5
  • 18