Pythonic 方式编写扑克函数以根据赢家规定经销商和盲注
Pythonic way to write a Poker function to prescribe dealer and blinds based on winner
我正在编写一个扑克项目,并且刚刚编写了一个函数来根据获胜者(赛前)来限制庄家、小盲注和大盲注。代码有效,但它看起来不像 pythonic:
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')
从指定索引开始遍历整个列表的最有效方法是什么?
您可以直接使用索引(mod 列表长度):
players = ['Jose', 'Sam', 'John', 'Victor']
winner = 'John'
dealer = players.index(winner)
small_blind = (dealer+1)%len(players)
big_blind = (dealer+2)%len(players)
我正在编写一个扑克项目,并且刚刚编写了一个函数来根据获胜者(赛前)来限制庄家、小盲注和大盲注。代码有效,但它看起来不像 pythonic:
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')
从指定索引开始遍历整个列表的最有效方法是什么?
您可以直接使用索引(mod 列表长度):
players = ['Jose', 'Sam', 'John', 'Victor']
winner = 'John'
dealer = players.index(winner)
small_blind = (dealer+1)%len(players)
big_blind = (dealer+2)%len(players)