循环直到(py)

时间:2016-12-15 22:31:01

标签: python

amount_of_names = int(input("Input amount of players: "))
names = []

for counter in range(amount_of_names):
  name = str(input("Input players first name: "))
  names.append(name)
#print (names)
selected_name = str(input("Input your name: "))

while name in names:
  import random 
  names = random.choice(names)
  break
print (names)

我正在尝试将列表中的名称与"您的姓名"匹配。并循环直到名称不匹配,例如Bob:Sam(是的)Sam:Sam(不) - 提前致谢

3 个答案:

答案 0 :(得分:0)

制作list,然后使用append为其添加输入。

AmountOfNames = int(input("Input amount of players: "))
Names = []

for counter in range (0, AmountOfNames):
    RegisteredName = input("Input player {}'s first name: ".format(counter+1))
    Names.append(RegisteredName)

print(Names)

示例:

Input amount of players: 3
Input player 1's first name: Simon
Input player 2's first name: James
Input player 3's first name: Jacob
['Simon', 'James', 'Jacob']

或者你可以这样做:

>>> names = input("Enter the first names of the players: ").split(", ")
Enter the first names of the players: Simon, James, Jacob
>>> names
['Simon', 'James', 'Jacob']

使用上述方法,如果他们每次都不用逗号和空格分隔它们,它会出现故障,所以要非常小心。

答案 1 :(得分:0)

试试这个:

double

现在,amount_of_names = int(input("Input amount of players: ")) names = [] for counter in range(amount_of_names): name = str(input("Input players first names: ")) names.append(name) print names 列表包含names名称

答案 2 :(得分:0)

在循环外面创建一个空数组。例如。 NameList = []然后,在循环内,对于每次迭代,将名称添加到使用NameList.append(// name)的列表中