2016-12-15 95 views
0
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(No) - 在此先感謝所有循環直到(py)

+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'] 

利用上述方法,如果他們不與他們逗號和空格每次分開,它會毛刺,所以要非常小心。

+0

是隻是一個硬編碼的方法?即時尋找更多的動態 –

+0

@RyanEvans你是什麼意思的動態? – rassar

+0

@RyanEvans查看最新的答案。你是這個意思嗎? – rassar

0

試試這個:現在

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列表包含amount_of_names

0

做一個空數組圈外的一面。例如。 NameList = []然後,在循環中,對於每次迭代,將名稱添加到使用NameList.append(//名稱)的列表中