[英]How to alternate output between two lists?
I am trying to output which card a player gets as the card is being handed out.我正在尝试 output 玩家在分发卡片时获得哪张卡片。 My idea was to output each card by alternating from the two lists.
我的想法是 output 每张卡通过交替从两个列表。 I have included an example below.
我在下面提供了一个示例。
Ex.
List1 = [Banana,Cherry,Orange,Apple,Tomato]
List2 = [Blue,Red,Orange,Yellow,Grey,Purple]
Output:
Banana
Blue
Cherry
Red
Orange
Yellow...
This is what I have so far.这就是我到目前为止所拥有的。
for card in player_A:
print("Player A is dealt= ",card,"")
for card in Player_B:
print("Player B is dealt=",card,"")
How can I go about doing that?我怎么能 go 这样做呢?
Use zip to alternate between the two lists.使用zip在两个列表之间交替。
for a, b in zip(List1, List2):
print(f'Player A is dealt= {a}')
print(f'Playber B is dealt= {b}')
Output Output
Player A is dealt= Banana
Playber B is dealt= Blue
Player A is dealt= Cherry
Playber B is dealt= Red
Player A is dealt= Orange
Playber B is dealt= Orange
Player A is dealt= Apple
Playber B is dealt= Yellow
Player A is dealt= Tomato
Playber B is dealt= Grey
You can just do你可以做
for i in range(len(List1)):
print("Player A is dealt= ", List1[i], "")
print("Player B is dealt= ", List2[i], "")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.