2016-12-15 4 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) 

リスト内の名前を「あなたの名前」と一致させようとしていて、名前が一致しなくなるまでループします。ボブ:サム(はい)サム:サム(いいえ) - 事前に感謝すべてLoop until(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(//名)useingリストに名前を追加