3 Replies - 3856 Views - Last Post: 11 August 2012 - 07:59 AM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 10-August 12

Getting hangman program to exit loop after guesses.

Posted 10 August 2012 - 03:12 PM

To start, here is my code:

# Hangman
print ("Let's play Hangman!")
print( "     O     \n"+"      |     \n" +"    \ /     \n" + "      |     \n"+"     /\     ")
category = input("Pick a Catergory: Animals, Places, People, Movies: ")


if category in ["Animals", "animals", "a", "A", "Animal", "animal","ANIMAL", "ANIMALS"]:
    picks = ["bird", "dog", "cat", "fish", "snake"]
    
    from random import choice
    word = choice(picks)
    print("Your word is " + str(len(word)) + " letters long")

    alreadyGuessed = []
    gameOver = False
    
    while gameOver == False:
        
            guess = input("What is your guess?  ")
            alreadyGuessed.append(guess)
            
            if guess in list(word):
                print ("Correct!...letters already guessed = " + str(alreadyGuessed))
                
            elif guess == word:
                print("Correct!...YOU WON!")
                gameOver = True
                
            else:
                print("Wrong Stupid!")
        
        


The problem i am having is when the user is guessing one letter at a time. It saves the letters guessed into the list alreadyGuessed, however, i can't seem to get the program to print YOU Won! and exit the loop once all the letters in the word have been guessed.

i tried to nest the an if statement into the first if statement, for example:

if list(word) in alreadyGuessed:
print ("you Won")
gameOver = True

and many different variations of this but still can't find a solution. I'm still learning python so i apologize if i my coding is sloppy.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting hangman program to exit loop after guesses.

#2 Sleeper256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-August 12

Re: Getting hangman program to exit loop after guesses.

Posted 11 August 2012 - 12:26 AM

Well one thing I notice so far is that you're using input instead of raw_input. Input returns an integer and raw_input returns a string. So that may cause problems.

Also, you variable "guess" only ever contains 1 letter right? The player's current guessed letter. So it will never equal the entire word.

I would add a new list, that you append only the correct letters to in the order of the word, and then each turn compare the joined version of that list to the word to see if you've won.


That's just what I notice though, I might be wrong I'm new too :P Welcome
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Getting hangman program to exit loop after guesses.

Posted 11 August 2012 - 06:39 AM

View PostSleeper256, on 11 August 2012 - 09:26 AM, said:

Well one thing I notice so far is that you're using input instead of raw_input. Input returns an integer and raw_input returns a string. So that may cause problems.


It looks like he's using Python 3 (he's surrounding the arguments to print with parentheses), so that doesn't apply.

Quote

I would add a new list, that you append only the correct letters to in the order of the word, and then each turn compare the joined version of that list to the word to see if you've won.


The "in the order of the word" bit is easier said then done. Especially if the word contains the same letter twice. What he could do is to put all the letters of the word into a set, then create a set with the correctly guessed letters and compare these sets for equality. The trick being that sets don't care about the order of their elements and don't contain duplicates.

@OP: One other thing you could do, that's more in line with what you've already tried, would be to loop over each character in word using a for-loop and check whether each one is in alreadyGuessed. If all of them are, the user one.

If you're already familiar with generator expressions (or list comprehensions), you can also use the all function instead of using a for-loop.

This post has been edited by sepp2k: 11 August 2012 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 10-August 12

Re: Getting hangman program to exit loop after guesses.

Posted 11 August 2012 - 07:59 AM

i figured it out finally thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1