-1

Everything works except the (current_space) does not get updated with the (current_space += rolled_dice). It just updates to the (rolled_dice) value. What causes this and are there better practices to implement here. Like a class?

from random import randint

finish_line = 20
player_1 = raw_input('Enter player 1 name: ')
player_2 = raw_input('Enter player 2 name: ')    
print('\nWelcome ' + player_1 + ' and ' + player_2)
print('\nLet\'s Play!\n')


def roll_1():
    current_space1 = int()
    #print(current_space1)
    roll_dice = raw_input(player_1 + ' roll dice? y or n: ')
    if roll_dice == 'y':
        rolled_dice = (randint(1,6))
        print(player_1 + ' ' + 'rolled a ' + str(rolled_dice))
        if current_space1 != finish_line:
            current_space1 += rolled_dice
            #print(current_space1)
            roll_2()
        elif current_space1 == finish_line:
            print('You are the winner ' + player_1 + '!')
        elif roll_dice == 'n':
            print('Thanks for playing')
        else:
            print('Invalid entry')



def roll_2():
    current_space2 = int()
    roll_dice = raw_input(player_2 + ' roll dice? y or n: ')
    if roll_dice == 'y':
        rolled_dice = (randint(1,6))
        print(player_2 + ' ' + 'rolled a ' + str(rolled_dice))
        if current_space2 != finish_line:
            current_space2 += rolled_dice
            roll_1()
        elif current_space2 == finish_line:
            print('You are the winner ' + player_2 + '!')
        elif roll_dice == 'n':
            print('Thanks for playing')
        else:
            print('Invalid entry')

roll_1() 
Josh
  • 5
  • 2
  • 5

2 Answers2

0

current_space1 and current_space2 are local variables of the functions roll_1 and roll_2 respectively. Those variables get actually incremented by the line ... += rolled_dice but in the next line you are invoking the function anew which in turn has its own scope where current_space is zero at the beginning. This will continue until you reach the max. recursion depth.

Instead you could make those variables global and mark them as such with the global keyword.

Another approach would be to create a Player class

class Player:
    def __init__(self):
        self.current_space = 0

and use only one roll function together with two Player instances which are passed as arguments to that roll function. You would then increment player.current_space += rolled_dice.

For example:

def roll(active_player, passive_player):
    ...
    if active_player.current_space != finish_line:
        active_player.current_space += rolled_dice
        roll(passive_player, active_player)

Btw you might also want to rethink when you check a player having reached the target because in your example you first ask to roll and then the player could win from a score which was obtained already in the turn before.

a_guest
  • 34,165
  • 12
  • 64
  • 118
0

Both current_space1 and current_space2 are local variables - they are created and initialized when the function enters and available only within the function context, once you leave the function, or enter another function, they are not available. If you want them to be available outside of the function (and increment within multiple function calls) you need to declare them globally:

finish_line = 20
player_1 = raw_input('Enter player 1 name: ')
player_2 = raw_input('Enter player 2 name: ')
current_space1 = 0
current_space2 = 0

# ...

def roll1():
    global current_space1  # instead of current_space1 = int()
    # rest of the function ...

def roll2():
    global current_space2  # instead of current_space2 = int()
    # rest of the function ...

Having said that, using global variable is not considered a good practice. For example, see this post and its answers.

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277