#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    11

    anoying thing to do


    i am doing a hangman game
    the thing is like this:
    i have the word called word
    i have the letter the user inputs

    well i want to generate a string thats contains the letter in its correct and the rest i want "_", and i did that, but i want to maintain this and sum the next input and that is what i cant

    since you probably didnt understand what i wrote i will make an example:

    the word is "elephant"
    i input letter e so the program will show me this:
    e _ e _ _ _ _ _
    and then i input "a" and the program should show this:
    e _ e _ _ a_ _
    but i cant do it, i am using too much "for" and "while" and "if" functions and i cant make it work

    thanks for your help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    I thought for a bit, and I can't see a nice way of doing it as you describe - summing the two (and more) seems quite hard as you are dealing with positions of letters, and so on.

    But you can do it quite neatly if you reverse it slightly - Instead of adding the letters as they are guessed correctly, hide the letters they have not yet guessed. So:


    Keep a store of correctly guessed letters so far.
    For each guess that is correct, add the letter to the store.
    Copy the word.
    Go through the letters in the word, and if the user has not yet guessed that letter, replace it with an underscore, e.g.

    Code:
    word = "elephant"
    letters = []
    
    # say the user guessed "e" last time:
    letters.append("e")
    
    # Now they guess "a":
    letter = "a"
    
    # Copy the word with all the letters visible
    output = word
    
    # Hide the letters that have not yet been guessed
    for each letter in output:
        if letter not in letters:
            output = output.replace(letter, "_")
    
    print output
    # output = "e_e__a__"
    [Edit: Did I really join this forum in November last year? ]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    11
    smart brain you have
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by ivanhope
    smart brain you have
    It's just where lots of reading of other people's suggestions on other forums has got me

IMN logo majestic logo threadwatch logo seochat tools logo