The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Hangman game help please =D
Discuss Hangman game help please =D in the Python Programming forum on Dev Shed. Hangman game help please =D Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 28th, 2012, 10:08 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 26 m 5 sec
Reputation Power: 0
|
|
|
Hangman game help please =D
Ok so here's my dilemma, I am currently reading 'Python Programming For the Absolute Beginner' (3rd edition) which is teaching me my first programming language, python 3.2.3. Now in this book at the end of one of the chapters there's a hangman game.
I've already been reading this book for a while a little at a time and already have a pretty good grasp on simple things like functions (what they are, and most notably the len() function), modules, while loops, if statements, for loops, lists, tuples, dictionaries, string methods, list methods, conditions, variable types, what sequences are, and mutability.
So now for my problem. I did not want to follow the books example on how to make the hangman game and instead tried to make it myself in IDLE. So I tried to make a while loop that took the players guess and made the exceptions for if a letter was already guessed and if a guess was wrong moving on to the next body part on the noose.
If the player guesses correctly I add the letter to an empty string. My problem is how do I take that variable for correct letters and display in the correct order what the player has guessed so far. I mean if I was being lazy I COULD just tell the player if they were right or wrong and just use if correctLetters in word: but I really want to learn how to manipulate strings to have all the sequences where I want them.
Any help is appreciated thanks =D
|

September 28th, 2012, 10:23 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 26 m 5 sec
Reputation Power: 0
|
|
|
If it helps
If it helps to understand what I'm talking about here's a sample of what my code would look like.
import random
WORDS = 'bunch of random words'.split()
HANGMAN = (pic1, pic2, pic3, pic4, pic5, pic6, pic7)
# ^Some ASCII art i made to show the noose and the stick man
word = random.choice(WORDS)
wrongGuesses = 0
guessedLetters = ''
guessedWord = ''
playGame = None
playGame = input('Press ENTER to exit or type in anything to play\n')
if playGame != '':
while wrongGuesses != 6
print(HANGMAN[wrongGuesses], '_'*len(word))
guess = input('Guess a letter')
if guess != guessedLetters and guess in word:
guessedWord += guess
guessedLetters += guess
elif guess in guessedLetters:
print('You already guessed that letter')
else:
print('That letter is not in the word')
wrongGuesses += 1
and that's where I'm stuck at =\
Last edited by monstahunta : September 28th, 2012 at 10:25 PM.
Reason: Fixed while loop
|

September 29th, 2012, 10:40 AM
|
 |
Contributing User
|
|
|
|
Code:
import sys # import usually at top of script
import random
# otherwise fail as soon as possible
if not input('Press ENTER to exit or type in anything to play\n'):
sys.exit(0)
HANGMAN = range(7) or (pic1, pic2, pic3, pic4, pic5, pic6, pic7)
# ^Some ASCII art i made to show the noose and the stick man
# let's not make case of the letter an issue. use lower case.
WORDS = 'bunch of random words'.lower().split()
word = random.choice(WORDS)
wrongGuesses = 0
guesses = set() # set is better data type
def substitutions(a,s,fill):
'''
a is an iterable with len method.
s is an object supporting the __contains__ method
fill is any object.
returns a list of length len(a) which has the fill value
substituted for any item of a not in s.
Learn also to write a function,
Learn to write a function of general use,
Learn to use doctest.
$ python3 -m doctest this_file.py # unix command
>>> # examples of our sterling substitutions function
>>> substitutions(list(range(8))+[4],set((4,3,),),666)
[666, 666, 666, 3, 4, 666, 666, 666, 4]
>>> substitutions('mississippi',set('xsuzp'),'_')
['_', '_', 's', 's', '_', 's', 's', '_', 'p', 'p', '_']
>>> ''.join(substitutions('mississippi',set('xsuzp'),'_'))
'__ss_ss_pp_'
>>>
'''
result = []
for i in a:
result.append(i if i in s else fill)
return result
while wrongGuesses != 6:
print(HANGMAN[wrongGuesses],''.join(substitutions(word,guesses,'_')))
guess = (input('Guess a letter')+' ')[0].lower() # one letter at a time.
if guess in guesses:
print('You already guessed that letter')
else:
guesses.add(guess)
if guess not in word:
print('That letter is not in the word')
wrongGuesses += 1
else:
print('more logic needed to determine final victory')
__________________
[code] Code tags[/code] are essential for python code!
|

September 29th, 2012, 10:21 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 1 h 26 m 5 sec
Reputation Power: 0
|
|
|
Thank You!
Thanks! My book doesn't cover doctest or the set data type so it helped alot. And my book always takes the long way around it seems since
if not input():
is a new concept to me that is total genius compared to what I've been doing. As far as making my own functions, I just started reading about that  It seems fascinating! I can't wait to get to classes! (My goal is to make a pokemon clone before next fall when I'm going to college to learn programming)
Also sorry about the codetags, I'm a noob and I normally read the FAQ's and stickies before posting on forums but I just figured that my spaces would automatically show up when I posted since I saw other posts where it showed 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|