#
#hangman.py
#

import random
import sys
wlist = ['apple', 'blue', 'house', 'frog']
guessed = []
gright = []
choice = None
word = random.choice(wlist)
guesses = 0
# add dictionary for category 


def menu():
    while True:
        print 'Welcome to hangman!'
        print 'To start a new game type "n"\n'
        print 'To quit the game press "q"\n'
        choice = raw_input('enter your choice: ')
        if choice == 'q':
            break
        if choice == 'n':
            return choice
        else:
            print 'invalid selection'
            continue 


def length(n):
    if len(n) > 1:
        return False
    else:
        return True
    
def chckguess():
    guesses = 0 
    n = raw_input('guess a letter: ')
    if n == 'q':
        sys.exit()
    else: 
        if n in word:
             if length(n) is True:
                 gright.append(n)
                 print 'correct!\n you have found the letters: %s' %gright
                 print 'you have used up %d of 5 wrong guesses so far' % guesses
                 return gright
        else:
            guesses += 1
            print 'sorry that letter is not in the word'
            return guesses
            




menu()
while True:
    print 'your word is %d letters long' % len(word)
    while guesses != 5:
        chckguess()
        if len(gright) == len(word):
            print 'you win!'  # later make user solve word
            sys.exit()
        else:
            continue

The problem i'm having is that the guesses don't go higher (after 5 wrong guesses the player should lose)

Can anyone help? or give any other comments about the code?

def chckguess(guesses)
#guesses = 0

By declaring guesses = 0 at the beginning of your function you are resetting its value. Also guesses being defined within the function only gives that variable scope within that function.
You should either have that variable be global, and then within the function call global guesses or simply pass the guesses variable from outside the function and then catch it on the return like so: guesses = chckguess(guesses) ; however keep in mind if you do this that you'll need to initialize guesses before your loop so that it exists.

Thanks for the help but could you give an example of one of them?

I'm new to python

Whenever I tried using the global thing i get errors and I don't understand what to do with the __init__
I defined it with guesses but i don't get what to do next

when i try the guesses = chckguesses(guesses) it says it's not defined

#
#hangman.py
#

import random
import sys
wlist = ['apple', 'blue', 'house', 'frog']
guessed = []
gright = []
guesses = 0
choice = None
word = random.choice(wlist)
# add dictionary for category 


def menu():
    while True:
        print 'Welcome to hangman!'
        print 'To start a new game type "n"\n'
        print 'To quit the game press "q"\n'
        choice = raw_input('enter your choice: ')
        if choice == 'q':
            break
        if choice == 'n':
            return choice
        else:
            print 'invalid selection'
            continue 


def length(n):
    if len(n) > 1:
        return False
    else:
        return True
    
def chckguess():
    global guesses
    n = raw_input('guess a letter: ')
    if n == 'q':
        sys.exit()
    else: 
        if n in word:
            if length(n) is True:
                gright.append(n)
                print 'correct!\n you have found the letters: %s' %gright
                print 'you have used up %d of 5 wrong guesses so far' % guesses
                return gright
        else:
            guesses += 1
            print 'sorry that letter is not in the word'


menu()
while True:
    print 'your word is %d letters long' % len(word)
    while guesses != 5:
        chckguess()
        if len(gright) == len(word):
            print 'you win!'  # later make user solve word
            sys.exit()
        else:
            continue

Here's an example of how to use the global method.

Let me also say that you seem to be mistaken in your usage of continue. You would use continue to skip the rest of the code within a loop, much like break, however you will continue onto the next iteration instead of completely breaking out of the loop.

You are also confused on the return statement. Return literally returns a value from a function. So by saying return x, there should be a variable waiting to catch that value at the function call.
Example:

>>> def funcA():
...     # This function returns a value
...     g = 5
...     return g
...     
>>> g = 100
>>> ret = funcA()
>>> g
100
>>> ret
5
>>>

See how g has a different scope within the function as it does outside the function? It's not the same variable just because it uses the same name. By saying return however, that variable is being sent back from the function to the main portion of code.

I hope that clears up some of your confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.