-1

I am making a word chain game in python but I am stuck at one place. The thing I wanted to do is

The game begins by asking how many people wish to play, and then prompting you to enter a name
for each of the players.

For this, I have created a code

def inputWord():
    Players = str(input("Enter Number of Players:"))
    Name = []
    WordChain = 0
    ls = list()
    PlayerNames = {}
    for i in range(0, int(Players)):
        Name = input("Enter Player Name:")
        PlayerNames = ls.append(Name)
        print(PlayerNames)
    print(PlayerNames)  

inputWord()

the Output that I am getting is 
Enter Number of Players:2
Enter Player Name:David
None
Enter Player Name:Martin
None
None

Instead, I need this

Enter Number of Players:2
Enter Player Name:David
David
Enter Player Name:Martin
Martin
[David, Martin] #list of the names for later use

I am new to python please help me.

Damian
  • 325
  • 1
  • 3
  • 7

1 Answers1

1

append is a python list method that is used to append new value to a existing list. The method does not return anything. When you are using:

PlayerNames = ls.append(Name)

The Name that you got from the user gets append to your list but it returns nothing. But you are trying to assign the returning value to PlayerNames variable and in that case the variable will be empty. So, whenever you are trying to print PlayerNames, it is showing None. Instead you already have user's name in your Name variable and you can use print(Name) to print the user's name on the screen.

Your Loop Should Be Like:

for i in range(0, int(Players)):
    Name = input("Enter Player Name:")
    ls.append(Name)      <-- append user's name to your list.
    print(Name)          <-- show what user has entered.
print(ls)                <-- print the whole list after the loop.

You should read more about python Data Structure. It has a good documentation.

https://docs.python.org/2/tutorial/datastructures.html

SK. Fazlee Rabby
  • 344
  • 4
  • 14