我得到了一份花色列表和一份等级列表。 我想编写一个函数来迭代它们,然后将每一对添加到一个元组中
SUITS = ["Hearts", "Diamonds", "Clubs", "Spades"]
RANKS = [
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
]
def create_deck(SUITS, RANKS):
combs_tpl = ()
for suit in SUITS:
for rank in RANKS:
combs_tpl = suit, rank
return combs_tpl
print(create_deck(SUITS, RANKS))
你只有一个元组,它在循环的每次迭代中都被覆盖。 相反,将每个元组附加到 list 并返回它。 例如:
combs_tpls = []
for suit in SUITS:
for rank in RANKS:
combs_tpls.append((suit, rank))
return combs_tpls