I am trying to make a party game on Python, and want the users to choose the number of players and set the amount of inputs of player names accordingly. For example if there are 3 players, the code must ask for 3 name inputs, if there are a 100 players, the code must ask for a 100 name inputs. Pls help.
Asked
Active
Viewed 90 times
-2
-
Just use a list – rizerphe Jun 28 '20 at 05:27
-
1If you need to associate any other information with the name variable, then you need to use a dictionary. – Trenton McKinney Jun 28 '20 at 05:39
-
There are many ways to solve this problem. There needs to be some design about what happens next after this data is entered. Is there a database of users? Is there other data to track? How will the game use this data? Here is a link to a [basic tutorial](https://docs.python.org/3/tutorial/index.html) but this is far to vague to answer, IMHO. – tdelaney Jun 28 '20 at 05:39
-
I think this directly answers your question: [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4518341) If you're still learning Python, you'll probably want to use a dict instead. But I agree with @tdelaney, more detail is needed. BTW welcome to SO! Please take the [tour] and read [ask]. – wjandrea Jun 28 '20 at 05:43
1 Answers
1
We store the total number of users in 't' variables. And loop till that number. each time we ask the user to provide input. Also, we storing the user input into the list using its append method.
lst = []
t = int(input("Enter number of player: "))
for i in range(t):
lst.append(input("Enter the name of player: "))

Astik Gabani
- 599
- 1
- 4
- 11
-
Please explain the key parts of your code example in words so that it will be more useful for future visitors. – Code-Apprentice Jun 28 '20 at 05:26
-
We store the total number of users in 't' variables. And loop till that number. each time we ask user to provide input. – Astik Gabani Jun 28 '20 at 05:28
-
Sorry for not being more clear with my request. I mean that you should edit your answer to include these details. In particular, you can emphasize how a list works to solve the OP's problem. Be sure to explain **why** the code solves the problem. – Code-Apprentice Jun 28 '20 at 05:31
-
@Code-Apprentice - that seems rather extreme in this case. Code can explain and what is happening here is very simple. If OP isn't at this level yet, then a tutorial or class on programming would be useful. I don't see how a treatise on lists would help here. If you want to do it, there is plenty of room below. – tdelaney Jun 28 '20 at 05:34
-
@tdelaney I'm not asking for a treatise on lists. I'm just asking for words to expound on the code example according to SO policy about "code only" answers. – Code-Apprentice Jun 28 '20 at 05:35
-
@tdelaney It doesn't need to be verbose, just a few sentences to explain how it works. What Astik added is fine. – wjandrea Jun 28 '20 at 05:40
-
-
You can loop through it and print the value. for i in lst: print(i) – Astik Gabani Jun 28 '20 at 10:04