0

In this example, we'll set PH[0] to 'Ten of Hearts', and guess to 'Ten'

The list 'PH' represents a Player's Hand in a card game of Go Fish. When the user guesses a card, they have to guess a card that corresponds with a card in their hand. I wrote this block of code so that if the user inputs an invalid guess (if the guess is not in PH, they will be prompted again to enter a new guess)

I feel like I am having problems with the variables in the array [guess1, guess2, guess3, guess4], but I am not too sure.

When the code is executed, the loop goes on forever. I need to be able to get out of the loop in order to return the guess so it can be entered into the next function.

If someone could please help me solve the problem I'm having.

guess = str(raw_input("Make a guess : "))

guess11 = guess, 'of Hearts'
guess1 = " ".join(guess11)

guess22 = guess, 'of Diamonds'
guess2 = " ".join(guess22)

guess33 = guess, 'of Clubs'
guess3 = " ".join(guess33)

guess44 = guess, 'of Spades'
guess4 = " ".join(guess44)

while PH[0] not in [guess1, guess2, guess3, guess4] :
    print "You do not have a card like that in your hand."
    guess = str(raw_input("Make another guess : "))

    guess11 = guess, 'of Hearts'
    guess1 = " ".join(guess11)

    guess22 = guess, 'of Diamonds'
    guess2 = " ".join(guess22)

    guess33 = guess, 'of Clubs'
    guess3 = " ".join(guess33)

    guess44 = guess, 'of Spades'
    guess4 = " ".join(guess44)

return guess
4
  • 5
    That code hurts my eyes! Learn about writing DRY code. It will make your life so much easier. Commented Sep 27, 2014 at 23:17
  • It works for me when I set PH[0] to 'Ten of Hearts' and guess to 'Ten'. The loop exits correctly. Commented Sep 27, 2014 at 23:18
  • raw_input is already a string Commented Sep 27, 2014 at 23:20
  • 1
    @Basic I will for sure look into that and learn how to make my code better. I am just beginning and learning. Thank you for the help Commented Sep 27, 2014 at 23:41

1 Answer 1

1

You are only testing if the first card in the players hand is any of he guesses. You need to test each card in the hand:

while not any(guess in PH for guess in [guess1, guess2, guess3, guess4]):

This takes each guessed card and tests that card against the hand in turn. any() stops looping over the guesses when a match is found.

A better idea still is to use set intersections:

guesses = {guess1, guess2, guess3, guess4}
while not guesses.intersection(PH):
    # ask for new guesses

You want to avoid having to type out your 'ask for a guess' code twice; start a loop with while True and use break to end the loop when a correct guess has been made:

suits = ('Hearts', 'Diamonds', 'Clubs', 'Spades')

while True:
    guess = raw_input("Make a guess:")
    guesses = {'{} of {}'.format(guess, suit) for suit in suits}
    if guesses.intersection(PH):
        # correct guess!
        break
    print "You do not have a card like that in your hand."

I used a set comprehension to build the guesses in a loop.

Sign up to request clarification or add additional context in comments.

1 Comment

@EvanCooper: but your while loop doesn't end until there is a matching card in PH[0]. Those other parts will never be reached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.