0

I have a person calling a function with three inputs:

  1. puzzle - so a random word, for example. 'john'
  2. view - their view of the puzzle (they guess letters and they become revealed scoring points, etc) so lets say they only see j^h^ (^ represents hidden characters).
  3. letter_guessed - they guess a letter, so if someone guessed 'o', the view would come back as 'joh^'

But my code just doesn't seem work and I can't seem to understand why, and please if you could do it using my bit of code below, I understand there are many ways to solve it but I'm interested in what I had to do if I wanted to solve this question using a for statement with nested if statements.

What it doesnt do: it simply displays the view again, the line of incorrect code is result = result + letter because i dont know how to make python scan for the hidden variable and replace the ^ with set found alphabetic letter.

def update_view(puzzle,view,letter_guessed):

    result = ""
    for index in range(0,len(puzzle)):
        if puzzle[index] == letter_guessed:
            result = result + letter_guessed
        else:           
            result = view
    return result 
5
  • You need to explain how it doesn't work. Does it give an error? Does it not do what you want? If so, what do you want it to do, and what does it do instead? Commented Oct 19, 2012 at 1:41
  • This is kind of a fun little game ... Now that I coded it up for myself, I just need to figure out a nice way to randomly get a word to play with ... Commented Oct 19, 2012 at 1:52
  • 1
    @mgilson: you could use the /usr/share/dict/words to pick a word Commented Oct 19, 2012 at 1:54
  • @xbonez -- brilliant. Now I have a new way to distract myself at work. I suppose I could call it hangman.py, although it currently lets me have infinite guesses... (though that would be trivial to fix) Commented Oct 19, 2012 at 2:00
  • stackoverflow.com/a/12926600/674039 Commented Oct 19, 2012 at 3:24

1 Answer 1

4

If the letter is currently "^" and the letter was guessed correctly, you want to add the guessed letter to result. Else, you want to add whatever was on the view earlier

def guess(word, view, letter) :
    result = ""
    for i in range(0,len(word)) :
        if view[i] == "^" and word[i] == letter:
            result += word[i]
        else :
            result += view[i]

    return result

Demo

The above if-else condition can be further shortened using Python's true if condition else false construct

def guess(word, view, letter) :
    result = ""
    for i in range(0,len(word)) :
        result += word[i] if view[i] == "^" and word[i] == letter else view[i]      
    return result
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.