0

I'm a noob, working on making the Yahtzee game I programmed multiplayer. In Python 2.7, I want to have a prompt where a user can enter the number of players, i.e. 2, 3,4, 2007 etc., after which for the number of players entered, i.e. 3, the user will enter the names of the players, i.e. Mike, Tom, Jim, which I can then use in my program to keep score (i.e. Mike's score is 7, he's pretty bad, Jim has 250, he's pretty good etc.). I've seen suggestions to use dictionaries, classes and arrays, but I'm lost as to which one is best, and worst still, can't make what I'm trying to do work.

from collections import defaultdict
d = defaultdict(int)

d = {}

players = raw_input('How many players?')
players = int(players)

for i in range (1,players+1):
  d = raw_input('Enter player name')

print d

my code on Repl.it is here

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
Mike
  • 201
  • 2
  • 14
  • You seem to be using `d` to be many different things; that might be part of why you can't make it work (you should explain what that means). – Scott Hunter Aug 26 '17 at 01:06
  • @Mike Because you've seen so many different suggestions is probably why it's confusing -- (maybe the question is off-topic because which one is "best" becomes a subjective discussion for some), and there are probably many reasonable solutions (class and dictionary), but since you've started a dictionary solution, you might stick with that if it makes sense, and there are lots of related resources that may help to complete it, here's one: [Python dictionary from an object's fields](https://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields) – chickity china chinese chicken Aug 26 '17 at 01:18

2 Answers2

0

In your for loop you are assigning whatever the player types in as a name to be equal to d. d is therefore instantly no longer referring to any dictionary, but a string.. whatever the name was. A particular variable can only refer to one object at a time. Logically this is straight-forward if you think about what happens when you re-assign a variable to something new, how could the interpreter possibly differentiate between multiple possible objects when all it's provided is the label d..

Try something like this, a dict seems good to me:

players_dict = {}

num_players = raw_input("Enter number of players: ")

for i in num_players:
    name = raw_input("Enter name for player %s:" % i)
    # set initial score to 0
    players_dict[name] = 0
Totem
  • 7,189
  • 5
  • 39
  • 66
0

Thanks all for the help. With your help, I figured out the following code (using a list and appending to it), which worked:

player_names = []

input_players = int(raw_input('how many players?'))

for i in range(0,input_players):
  name = raw_input('enter player name')
  name = name.upper()
  player_names.append(name)

#using my new player names to iterate through turns:

for i in range(0,13):
  for i in player_names:
    print i # placeholder for my turn function 
Mike
  • 201
  • 2
  • 14