파이썬에서 모노 폴리을 쓰기로 결정했지만 플레이어 위치를 업데이트하는 데 문제가 있습니다. 나는 for 루프를 작성하여 플레이어를 반복하고 각 주사위를 굴린 다음 위치를 업데이트합니다. 문제는 위치 변수가 최신 위치를 유지하지 못하고 for 루프가 시작될 때 0으로 재설정을 계속 유지한다는 것입니다. 여기에 내 코드입니다 : 필요한 경우파이썬 - For 루프에서 변수 업데이트
가player1location = 0
def turn(numberPlayers, player, player1location, Board):
for player in range(numberPlayers):
player = 'Player'+str(player+1)
print 'It\'s', player, 'turn!'
print player1location
rollDice = raw_input('Press Enter to roll the dice!')
diceRoll = random.randint(1,6)
print player, 'rolled a', diceRoll
player1location = player1location + diceRoll
print 'You landed on', player1location
print '\n'
while True:
turn(numberPlayers, player, player1location, Board)
내가 코드의 자세한 내용을 제공 할 수 있습니다,하지만 난이 선수의 위치를 제어 모든 생각합니다. 감사!
EDIT : 그래서 분명히 전역 변수 대신 로컬 변수가 변경됩니다. 대신 전역 변수를 어떻게 변경합니까?
루프 내에서 전역 'player1location'을 사용하고있는 동안 함수를 변경하면 범위를 종료 할 때 버려지는 로컬 변수가 변경됩니다. – geoffspear
전역 변수를 사용할 때'global' 키워드를 사용해야할까요? – gustavodidomenico
(왜, 플레이어 1 명을 추적하고 게임에서 각 플레이어를 이동시키는 것입니까?) – geoffspear