'Passing information between while True statments in python
''' I am trying to figure out how to pass information between two while True statements. Basically when player1 asks for a card, I want to store that card in a list of remembered cards for player2. That way when it is player2's turn it can check its hand for that card and if it has it, it will ask for that card, otherwise; the random.choice function will pick the card. Next I want to make my player2 remember the cards it asks for that player1 doesn't have so I can prevent player2 from asking for that card for a certain amount of turns. I am basically trying to program my computer player to be smarter. '''
remembered_cards = set()
for_player_2 = set()
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.add(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
cardFound = False
#This is where I need to fix
while cardFound:
for x in for_player_2:
for j in player_2_hand:
if x[0] == j[0]:
cardFound == True
player_2_choice = j
print("Player 2 asked for ", player_2_choice)
break
if not cardFound:
player_2_choice = random.choice(player_2_hand)
print("Player 2 asked for ", player_2_choice)
if player_2_choice not in player_2_hand:
continue
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
Solution 1:[1]
you can initialize the for_player_2 variable outside the loop then modify it within the loop.
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
##HERE## <<============
card_found = False # a flag to be used later
for card in for_player_2:
for hand_card in player_2_hand:
if hand_card[0] == card[0]:
card = hand_card
# ask for that card
# NB: I don't know what you mean by "ask for that card", so I will print it instead
print(card)
player_2_choice = card
card_found = True
break # exit the for loop
if not card_found: # if card_found is still False (ie: if block inside for loop was not executed. ie: card was not found)
player_2_choice = random.choice(player_2_hand) # <<<======== your old function
#########################
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
