Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 28th, 2012, 10:08 PM
monstahunta monstahunta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 monstahunta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old September 28th, 2012, 10:23 PM
monstahunta monstahunta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 monstahunta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old September 29th, 2012, 10:40 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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!

Reply With Quote
  #4  
Old September 29th, 2012, 10:21 PM
monstahunta monstahunta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 monstahunta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Hangman game help please =D

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap