User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,097 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,324 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 219 | Replies: 4
Reply
Join Date: Jun 2008
Posts: 12
Reputation: thatoneguyx is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
thatoneguyx thatoneguyx is offline Offline
Newbie Poster

word game help

  #1  
Jul 22nd, 2008
 
#
#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?
Last edited by thatoneguyx : Jul 22nd, 2008 at 6:29 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: word game help

  #2  
Jul 22nd, 2008
  1.  
  2. def chckguess(guesses)
  3. #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.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Jun 2008
Posts: 12
Reputation: thatoneguyx is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
thatoneguyx thatoneguyx is offline Offline
Newbie Poster

Re: word game help

  #3  
Jul 22nd, 2008
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
Last edited by thatoneguyx : Jul 22nd, 2008 at 7:20 pm.
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: word game help

  #4  
Jul 23rd, 2008
  1. #
  2. #hangman.py
  3. #
  4.  
  5. import random
  6. import sys
  7. wlist = ['apple', 'blue', 'house', 'frog']
  8. guessed = []
  9. gright = []
  10. guesses = 0
  11. choice = None
  12. word = random.choice(wlist)
  13. # add dictionary for category
  14.  
  15.  
  16. def menu():
  17. while True:
  18. print 'Welcome to hangman!'
  19. print 'To start a new game type "n"\n'
  20. print 'To quit the game press "q"\n'
  21. choice = raw_input('enter your choice: ')
  22. if choice == 'q':
  23. break
  24. if choice == 'n':
  25. return choice
  26. else:
  27. print 'invalid selection'
  28. continue
  29.  
  30.  
  31. def length(n):
  32. if len(n) > 1:
  33. return False
  34. else:
  35. return True
  36.  
  37. def chckguess():
  38. global guesses
  39. n = raw_input('guess a letter: ')
  40. if n == 'q':
  41. sys.exit()
  42. else:
  43. if n in word:
  44. if length(n) is True:
  45. gright.append(n)
  46. print 'correct!\n you have found the letters: %s' %gright
  47. print 'you have used up %d of 5 wrong guesses so far' % guesses
  48. return gright
  49. else:
  50. guesses += 1
  51. print 'sorry that letter is not in the word'
  52.  
  53.  
  54. menu()
  55. while True:
  56. print 'your word is %d letters long' % len(word)
  57. while guesses != 5:
  58. chckguess()
  59. if len(gright) == len(word):
  60. print 'you win!' # later make user solve word
  61. sys.exit()
  62. else:
  63. continue

Here's an example of how to use the global method.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: word game help

  #5  
Jul 23rd, 2008
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:
  1. >>> def funcA():
  2. ... # This function returns a value
  3. ... g = 5
  4. ... return g
  5. ...
  6. >>> g = 100
  7. >>> ret = funcA()
  8. >>> g
  9. 100
  10. >>> ret
  11. 5
  12. >>>
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.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 5:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC