vendredi 5 mai 2017

How to add to and update variables in an if/elif statement in Python?

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() 

Aucun commentaire:

Enregistrer un commentaire