Python Forum
Loop problems - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loop problems (/thread-1923.html)



Loop problems - yuvalsaias - Feb-04-2017

Hello everyone.
im new to python and im trying to make a snakes and ladder game code.
im having troubles as you can see at the code below with the 'for' loop.
its calculate the steps a player move each time he roll the dice but the current step he is at the end of the loop save it as the start step for the next player and than there is a situation when a player is on the 5th step and get to the 15th step in a turn when you can only jump from 1-6 steps a turn.
please tell me what im doing wrong and how can i fix that, thank you:)
sorry for my english as well if i have mistakes.

import random
num_of_user = int(input('how many players do you want?:'))
users = []
user_name = ''
for i in range(1, num_of_user+1):
   user_name = input ('what is the name of player' + str(i) +'?')
   users.append(user_name)
step = 0
while step < 100 :
   for user in users:
       start = input(user +  'Whould you like to roll the dice? Yes or No ')
       if start == 'yes' :
           dice = random.randint(1, 6)
           current_step = step + dice
           step = current_step
           print (user , 'is currently on step' , current_step)



RE: Loop problems - ichabod801 - Feb-04-2017

You need to keep track of the location for each user. You are currently tracking only one location, so each time one player moves, every player moves. I think the best way to track locations for each player would be a dictionary. The keys would be the user names, and the values would be the steps they are on. A dictionary would also work very well for handling the snakes and ladders when you get to that point.

If you are familiar with dictionaries, give it a try, and I can help you if you have problems. If you are not familiar with dictionaries, there is a tutorial on them on this site. Read that, give it a try, and I can help you if you have problems.