在python中使用布尔列表(working with a boolean list in python)
我正在上课的锦标赛计划。 该程序应该让用户输入所有团队名称,选择2个团队,并询问用户赢得哪个团队,获胜者将继续前进。 我想通过使用布尔值在一个数组中完成所有这些操作。 我希望数组中的所有值都以false开头,如果他们赢了,那么团队名称将变为true 。
到目前为止,我有这个
amount = int(raw_input('How many teams are playing in this tournament? ')
teams = []
i = 0
while i < amount:
teams.append(raw_input("please enter team name: ")
i = i + 1
现在,我怎么能把整个列表false ?
I am working on a tournament program for class. The program is supposed to have the user input all of the team names, select 2 teams, and ask the user which team won, and the winner will move on. I want to do this all in one array by using boolean values. I want all of the values in the array to start off as false, and if they win that team name turns to true.
So far I have this
amount = int(raw_input('How many teams are playing in this tournament? ')
teams = []
i = 0
while i < amount:
teams.append(raw_input("please enter team name: ")
i = i + 1
Now, how can I make the whole list false?
原文:https://stackoverflow.com/questions/15770282
2020-07-21 20:07
满意答案
在我看来,使用字典而不是列表是一种更好的方法。 您只需将每个团队名称添加为字典的键,并将其对应的值分别设置为False或True :
amount = int(raw_input('How many teams are playing in this tournament? ')
teams = {}
i = 0
while i < amount:
team_name = raw_input("please enter team name: ")
teams[team_name] = False
i = i + 1
如果您想选择赢得比赛的团队,您只需在字典中进行团队名称查找并将其