输入以从列表中选择项目

时间:2016-10-24 13:19:17

标签: python list

我正在制作排名系统,我想知道你是否可以使用输入从列表中选择一个项目。如果输入与列表中的任何项匹配,则可以为其分配点。

这是我到目前为止所得到的:

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!.

我做错了什么?

1 个答案:

答案 0 :(得分:-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!")