首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据胜利者编写扑克函数的Pythonic方法来开经销商和百叶窗

根据胜利者编写扑克函数的Pythonic方法来开经销商和百叶窗
EN

Stack Overflow用户
提问于 2022-02-19 14:14:44
回答 1查看 63关注 0票数 0

我正在编写一个扑克项目,并刚刚编写了一个函数来重新描述经销商,小百叶窗和大百叶窗的基础上(赛前)。代码工作正常,但看上去不像pythonic:

代码语言:javascript
复制
players = {1:'Jose',2:'Sam',3:'John',4:'Victor'}
## Each player receives a card, John gets an Ace and wins the dealer spot
winner = 'John'

for num in players:
    if players[num] == winner:
        dealer = num
        
        if len(players) == 2:
            if num+1 <= len(players):
                small_blind = num+1
            else:
                small_blind = 1
                
        elif len(players) >= 3:
            if num+1 <=len(players):
                small_blind = num+1
                if num+2 <= len(players):
                    big_blind = num+2
                else:
                    big_blind = 1
            else:
                small_blind = 1
                big_blind = 2

print(f'{players[dealer]} will be dealing cards')
print(f'{players[small_blind]} will be small blind')
print(f'{players[big_blind]} will be big blind')

从指定索引开始循环整个列表的最有效方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-19 14:33:05

您可以直接使用索引(列表的mod长度):

代码语言:javascript
复制
players = ['Jose', 'Sam', 'John', 'Victor']
winner = 'John'

dealer = players.index(winner)
small_blind = (dealer+1)%len(players)
big_blind   = (dealer+2)%len(players)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71185784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档