我們可以傳遞玩家分數和玩家名稱的元組。由於這些啓動比分,我們可以呼籲他們min()
因爲如果他們的數字:
def round1(players):
loser = min(*players)
print("The loser is:", loser[1])
remaining = [player for player in players if player != loser]
round2(remaining)
def round2(players):
print(", ".join(player[1] for player in players), "are still in the game.")
print("Are you ready for the next scores?")
rod = int(input("Please enter Rod's score: "))
jane = int(input("Please enter Jane's score: "))
freddy = int(input("Please enter Freddy's score: "))
round1([(rod, 'Rod'), (jane, 'Jane'), (freddy, 'Freddy')])
用法
% python3 test.py
Please enter Rod's score: 13
Please enter Jane's score: 34
Please enter Freddy's score: 56
The loser is: Rod
Jane, Freddy are still in the game.
Are you ready for the next scores?
%
我怎麼可能讓前兩名經歷只能用分?
我假設你不想讓列表理解分離出剩下的玩家。我們可以擺脫它的方法之一,是通過players
一個set
代替list
:
def round1(players):
loser = min(*players)
print("The loser is:", loser[1])
round2(players - set(loser))
def round2(players):
print(", ".join(player[1] for player in players), "are still in the game.")
print("Are you ready for the next scores?")
rod = int(input("Please enter Rod's score: "))
jane = int(input("Please enter Jane's score: "))
freddy = int(input("Please enter Freddy's score: "))
round1({(rod, 'Rod'), (jane, 'Jane'), (freddy, 'Freddy')})
這是有道理的,因爲有暗示沒有特定的順序。
請用編程語言標記問題,而不是語言關鍵字。 –
我希望我知道你的意思 – user3631273