•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 0
#
#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.
python Syntax (Toggle Plain Text)
def chckguess(guesses) #guesses = 0
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. **
** 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. **
•
•
Join Date: Jun 2008
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 0
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
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.
python Syntax (Toggle Plain Text)
# #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'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. **
** 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. **
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:
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.
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:
python Syntax (Toggle Plain Text)
>>> def funcA(): ... # This function returns a value ... g = 5 ... return g ... >>> g = 100 >>> ret = funcA() >>> g 100 >>> ret 5 >>>
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. **
** 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. **
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Fantastic word game (Posting Games)
- Fantastic Word Game-2 (Posting Games)
- POLL: Your Favorite Daniweb Word Game (Geeks' Lounge)
- Kami's Word Game (Geeks' Lounge)
Other Threads in the Python Forum
- Previous Thread: how can i set scroll bar to form in Qt Designer
- Next Thread: Need a guiding hand


Linear Mode