Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop problems
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with my loop python_student 3 1,506 Feb-17-2022, 09:49 PM
Last Post: deanhystad
  sports Stats > table output loop problems paulfearn100 3 2,444 Jul-22-2020, 03:21 AM
Last Post: c_rutherford
  Loop problems Kristenl2784 0 1,315 Jul-09-2020, 03:57 PM
Last Post: Kristenl2784
  “While” Loop Problems Ryan_teresi 3 2,822 Jun-27-2020, 06:43 PM
Last Post: tanv321

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020