Python:如何从用户获取多个值?
我正在用python制作一个单词链游戏,但我被困在一个地方。 我想做的是Python:如何从用户获取多个值?,python,Python,我正在用python制作一个单词链游戏,但我被困在一个地方。 我想做的是 The game begins by asking how many people wish to play, and then prompting you to enter a name for each of the players. 为此,我创建了一个代码 def inputWord(): Players = str(input("Enter Number of Players:")) Name =
The game begins by asking how many people wish to play, and then prompting you to enter a name
for each of the players.
为此,我创建了一个代码
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
相反,我需要这个
Enter Number of Players:2
Enter Player Name:David
David
Enter Player Name:Martin
Martin
[David, Martin] #list of the names for later use
我是python新手,请帮帮我。
append
是一种python列表方法,用于将新值附加到现有列表中。该方法不返回任何内容。使用时:
PlayerNames = ls.append(Name)
从用户处获得的名称
会附加到列表中,但它不会返回任何内容。但是您试图将返回值分配给PlayerNames
变量,在这种情况下,该变量将为空。因此,每当您试图打印PlayerNames
,它都会显示None
。相反,您的name
变量中已经有用户名,您可以使用print(name)
在屏幕上打印用户名
您的循环应该是这样的:
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.
范围内的i(0,int(玩家)):
名称=输入(“输入玩家名称:”)
ls.append(Name)print(Name)
然后print(ls)
可能重复谢谢Julien。这解决了我的问题,谢谢你。法兹利·拉比。