在python中的while True语句之间传递信息
python 181
原文标题 :Passing information between while True statments in python
”’我试图弄清楚如何在两个真语句之间传递信息。基本上,当玩家 1 要求一张卡片时,我想将该卡片存储在玩家 2 的记住卡片列表中。这样当轮到玩家 2 时可以检查它的手牌,如果它有,它会要求那张牌,否则; random.choice 函数会选择这张牌。接下来我想让我的玩家 2 记住它要求玩家 1 没有的牌,这样我可以防止玩家 2 在一定的回合内要求这张牌。我基本上是在尝试让我的电脑播放器变得更聪明。”’
for_player_2 = []
while True:
if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0:
break
# Player 1's turn
while True:
if len(player_1_hand) == 0:
break
print("Your cards ", player_1_pairs)
desired_card = input("What card would you like?\n").upper()
# Putting this here to try to help describe what I am trying to do
for_player_2 = []
for_player_2 += desired_card
if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs)== True:
print("Well done")
else:
print("Go Fish")
player_1_had.append(deal_top_card(deck))
drawn_card = player_1_hand[-1]
if check_d_card(player_1_hand, player_1_pairs, desired_card, drawn_card)== Tr:
print("Congratulations, you drew the match to your pair!!!")
continue
elif check_d_card(player_1_hand, player_1_pairs, desired_card, drawn_card)==F:
for i in range(len(player_1_hand)):
for j in range(i + 1, len(player_1_hand):
# If card ranks match
card1 = player_1_hand[i]
card2 = player_1_hand[j]
if card1[0] == card2[0]:
player_1_pairs.extend([card1, card2])
player_1_hand.remove(card1)
player_1_hand.remove(card2)
break
break
#Player 2's turn
while True:
if len(player_2_hand) == 0:
break
# This is where I want to run a check to see if the card player1 just asked for is in player2's hand and if it is, player2 will ask for it, otherwise; it will run the random.choice function to pick the card.
# Also I want to figure out how to remember when player2 asks for a card and player1 doesn't have it so I can prevent player2 from asking for it again for a certain number of turns if possible.
player_2_choice = random.choice(player_2_hand) # I want to prevent the remembered wrong guesses for player2 from being a choice for a certain number of turns.
if player_2_choice not in player_2_hand:
continue
print("Player 2 asked for ", player_2_choice)
if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs)==Tr:
print("Player 2 got a pat on the back for guessing correctly.")
else:
print("Player 2 had to go fishing!!!")
drawn_card = player_2_hand[-1]
if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card)==True:
print("Player 2 got a pat on the back for drawing their pair")
elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card)==F:
for i in range(len(player_2_hand)):
for j in range(i + 1, len(player_2_hand):
# If card ranks match
card1 = player_2_hand[i]
card2 = player_2_hand[j]
if card1[0] == card2[0]:
player_2_pairs.extend([card1, card2])
player_2_hand.remove(card1)
player_2_hand.remove(card2)
break
break
回复
我来回复-
ibrahem 评论
您可以在循环外初始化
for_player_2
变量,然后在循环内对其进行修改。for_player_2 = [] # <<========================= initialize variable here while True: if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0: break # Player 1's turn while True: if len(player_1_hand) == 0: break print("Your cards ", player_1_pairs) desired_card = input("What card would you like?\n").upper() # Putting this here to try to help describe what I am trying to do # for_player_2 = [] # <<==== remove this line for_player_2 += desired_card if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs)== True: print("Well done") else: print("Go Fish") player_1_had.append(deal_top_card(deck)) drawn_card = player_1_hand[-1] if check_d_card(player_1_hand, player_1_pairs, desired_card, drawn_card)== Tr: print("Congratulations, you drew the match to your pair!!!") continue elif check_d_card(player_1_hand, player_1_pairs, desired_card, drawn_card)==F: for i in range(len(player_1_hand)): for j in range(i + 1, len(player_1_hand): # If card ranks match card1 = player_1_hand[i] card2 = player_1_hand[j] if card1[0] == card2[0]: player_1_pairs.extend([card1, card2]) player_1_hand.remove(card1) player_1_hand.remove(card2) break break #Player 2's turn while True: if len(player_2_hand) == 0: break # This is the list that I am trying to limit the scope of print(for_player_2) # <<====== the variable is accessible here player_2_choice = random.choice(player_2_hand) # I want to subtract for_player_2 if player_2_choice not in player_2_hand: continue print("Player 2 asked for ", player_2_choice) if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs)==Tr: print("Player 2 got a pat on the back for guessing correctly.") else: print("Player 2 had to go fishing!!!") drawn_card = player_2_hand[-1] if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card)==True: print("Player 2 got a pat on the back for drawing their pair") elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card)==F: for i in range(len(player_2_hand)): for j in range(i + 1, len(player_2_hand): # If card ranks match card1 = player_2_hand[i] card2 = player_2_hand[j] if card1[0] == card2[0]: player_2_pairs.extend([card1, card2]) player_2_hand.remove(card1) player_2_hand.remove(card2) break break
2年前