Jump to content
  • Advertisement
Sign in to follow this  
revisioned

python question (hangman game)

This topic is 3643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a hangman game and I'm stuck -- I'm trying to write a condition where if a user already guessed a letter, it tells them they already guessed it and if it's right it doesn't add it to the letters found again. I tried this:
if guess in word:
                if guess in right: # check if already guessed
                    print "you already guessed that letter"
                    guess = raw_input('Guess a letter: ')
right is the letters the user has already correctly guessed but python can't seem to check this since right isn't an integer (i'm still learning python basics i don't know if i'm saying this right...) If it helps my whole code so far is:
import random
#------------------------------
# word list 
wordlist = ['blue', 'tree']  #possible words
#------------------------------

#window start
print '********************************************'
print 'Welcome to hangman with a twist --'
print 'While you still have to guess the letters, you also'
print 'have to put them together in the end~'
print '********************************************'
print 'To play type "play"'
print 'To quit type "quit"'

game = raw_input("Enter your selection: ")

if game == 'play':
    right = 0
    print "First we'll figure out your word:\n-----\n"
    word = random.choice(wordlist)   # gets a random word from wordlist
    while right != word: 
        if len(word) == 4:
            blanks = '_ _ _ _'
            print 'your word is %s letters long\n' % len(word), blanks
            guess = raw_input('Guess a letter: ')
            # check if the guess is right
        
            if guess in word:
                if guess in right: # check if already guessed
                    print "you already guessed that letter"
                    guess = raw_input('Guess a letter: ')
                else: 
                    print 'Correct! "%s" is in the word' % guess
                    right = (guess)    # put letters picked right in here
                    print 'letter you have gotten so far: "%s" \n' % right
                    guess = raw_input('Guess a letter: ')
                
                
                
            
        if len(word) == 5:
            blanks = '_ _ _ _ _'
            print 'your word is %s letters long\n' % len(word), blanks
            
        if len(word) == 6:
            blanks = '_ _ _ _ _ _'
            print 'your word is %s letters long\n' % len(word), blanks
            
        if len(word) == 7:
            blanks = '_ _ _ _ _ _ _'
            print 'your word is %s letters long\n' % len(word), blanks
        
        

elif game == 'quit':
    pass

else:
    print 'choose to either play or quit, press "play" to play or "quit" to quit'
    game = raw_input("enter your selection: ")

    
    


I know it's very simple but I don't care any help or tips would be great

Share this post


Link to post
Share on other sites
Advertisement
Make right a list instead of an integer:
right = []

Then you can use the syntax to check if the guess is in the list of previous correctly guessed letters:
if guess in right:

And when the user makes a new correct guess, you add it to the list:
if guess in word:
if guess in right: # check if already guessed
...
else:
...
right.append(guess) # put letters picked right in here

Share this post


Link to post
Share on other sites
Quote:

right is the letters the user has already correctly guessed
but python can't seem to check this since right isn't an integer (i'm still learning python basics i don't know if i'm saying this right...)


Not quite. The problem is that right is an integer. You want it to be a string or a list of characters.

You first assigned it like so...

right = 0

You probably want an empty string here...

right = ""

You tried to add a guess like this...

right = (guess)

That just reassigns right to be the same as guess. You want to append...

right += guess

There are a number of other things wrong with your code design. For example you duplicate a lot of things that should only be written once. An if statement for every possible length of word? Three identical raw_input statements? Try to refactor these things so they only appear once. (I'd do it for you but then what's the point of learning [smile])

Share this post


Link to post
Share on other sites
i dont know why i didn't think to make it a list >.<

Thanks guys

As for the repeating things I want to fix it but right now this is the easiest for me and to read-- I'll try to fix it up once I get some of it working :)

Share this post


Link to post
Share on other sites
Quote:
Original post by revisioned
i dont know why i didn't think to make it a list >.<


Because you aren't fully accustomed to the concepts yet. But basically, a container is something which can have other things 'in' it, and a list is one of those.

Quote:
As for the repeating things I want to fix it but right now this is the easiest for me and to read-- I'll try to fix it up once I get some of it working :)


To advance as a programmer, you pretty much have to be able to look at the "fixed" version, and not have any doubt that it's easier to read *that* way. :) Programming is all about generalization: i.e., identifying what things will be true/the same no matter what, and which need to vary; and expressing the variances as neatly as possible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!