2016-10-24 16 views
-4

我在做一個排名系統,我想知道是否可以使用輸入從列表中選擇一個項目。如果輸入與列表中的任何項目相匹配,則可以爲其分配點。輸入來從列表中選擇項目

這是我走到這一步:

teams = list() 
scores = list() 

#adding a team 
if loop=="1": 
    team_name = input("Enter a team name: ") 
    print ("This team is succesfully added!") 
    teams.append(team_name) 

#selecting the team by user input 
elif loop=="4": 
    test = input("Enter a team name: ") 
    if test is any in list(teams): 
     score_team = int(input("How many points does this team get? ")) 
     scores.append(score_team) 
    else: 
     print("Sorry, thats not a valid team name!") 

我總是得到輸出:

Sorry, thats not a valid team name!. 

我在做什麼錯?

+3

'如果測試是任何在列表(隊)'。這不符合你的想法。您正在檢查'test'是否是函數'any',然後檢查該表達式的結果是否在'teams'中(您將從列表轉換爲列表)。 –

+1

'如果在團隊中測試:'是你所需要的全部 –

+1

'如果團隊測試:' –

回答

-1

只需檢查列表是否包含用戶使用'in'關鍵字輸入的團隊。所以它應該工作

teams = list() 
scores = list() 

#adding a team 
if loop=="1": 
    team_name = input("Enter a team name: ") 
    print ("This team is succesfully added!") 
    teams.append(team_name) 

#selecting the team by user input 
elif loop=="4": 
    test = input("Enter a team name: ") 
    if test in list(teams): 
     score_team = int(input("How many points does this team get? ")) 
     scores.append(score_team) 
    else: 
     print("Sorry, thats not a valid team name!")