0

I'm creating a loop which looks a it like this

players = int(input('How many players? '))
for i in range (0,players):
   name = input('What is your name? ')

Then I want the next variable to be 'value_of_' and then whatever they inputed as their name will be in the name of the variable.

  • To clarify, if you type your name as "Joe" then you want a variable Joe whose value is "Joe"? –  Dec 18 '15 at 23:06

2 Answers2

2

You need to store that input-values somewhere, in a list for example. A concise way to achieve what you want would be:

playernames = [input('What is your name? ') for _ in range(players)]

You could also use a dictionary to map the string 'player_nr_X' to the playernames like this:

playernames = {'player_nr_' + str(i + 1) : input('What is your name? ') for i in range(players)}

Of course, you could also simply use integer keys in your dictionary.

Demo:

>>> players = 3
>>> {'player_nr_' + str(i + 1) : input('What is your name? ') for i in range(players)}
What is your name? 'bob'
What is your name? 'alice'
What is your name? 'jeff'
{'player_nr_1': 'bob', 'player_nr_2': 'alice', 'player_nr_3': 'jeff'}    
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

computed variable names are a bad idea, they make writing code more difficult. What you want to do instead is use a dictionary where the keys are the names.

For example:

values = {}
players = int(input('How many players? '))
for i in range (0,players):
   name = input('What is your name? ')
   value[name] = i

Or, if you want to use the index value as the key, and the name as the value:

value[i] = name
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685