python boolean list_在python中使用布尔列表(working with a boolean list in python)

在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

如果您想选择赢得比赛的团队,您只需在字典中进行团队名称查找并将其

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值