-4

I have had this problem for a while and can't seem to find anything that works. How would I get it so that currentspace is increased by the total amount rolled.

def roll():
    import random
    doubles = 0

    x = random.randint(1, 6)
    y = random.randint(1, 6)

    print(x, y)
    if x == y:
        print('Doubles')
        doubles = 1
        print(f'You have rolled {doubles} doubles!')
        x1 = random.randint(1, 6)
        y1 = random.randint(1, 6)
        print(x1, y1)
        if x1 == y1:
            print('Doubles')
            doubles = 2
            print(f'You have rolled {doubles} doubles!')
            x2 = random.randint(1, 6)
            y2 = random.randint(1, 6)
            print(x2, y2)
            if x2 == y2:
                doubles = 3
                print(f'You rolled {doubles} doubles!')
                print('Go to Jail!')
            else:
                total2 = x + y + x1 + y1 + x2 + y2
                print(f'You rolled {total2} in total.')

        else:
            total1 = x + y + x1 + y1
            print(f'You rolled {total1} in total.')

    else:
        total = x + y
        print(f'You rolled {total} in total.')
        


currentspace = 0

print(roll())
Julien
  • 13,986
  • 5
  • 29
  • 53
NoahAllan
  • 23
  • 3

1 Answers1

0

Use global statement.

def roll():
    global currentspace
    ...do your things...
    currentspace += 1
go2nirvana
  • 1,598
  • 11
  • 20
  • It still is not working. I am getting this warning: Global variable 'currentspace' is undefined at the module level Inspection info: This inspection is used when a variable is defined through the "global" statement but the variable is not defined in the module scope. And this syntax: Traceback (most recent call last): File "C:/#####.py", line 50, in print(roll(callback= currentspace)) NameError: name 'currentspace' is not defined – NoahAllan Sep 15 '20 at 10:09
  • You have to define it before function. – go2nirvana Sep 15 '20 at 10:24