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

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2

    Need HELP with this function!


    In this function i have 3 strings...

    def update_view (puzzle, view, guessed_letter):
    ''' (str, str, str) -> str

    Return the view of the puzzle with each occurrence of the letter in
    the puzzle revealed.

    >>>update_view ('apple', '^^^^^', 'a')
    'a^^^^'
    >>>update_view ('apple', 'a^^^^', 'p')
    'app^^'

    '''

    My function so far is:
    result = ""
    for i in range(len(puzzle)):
    c = puzzle[i]
    if c.islower() and c not in guessed_letter:
    result+= HIDDEN
    else:
    result = result + c
    return result

    But this is not returning the 'view' parameter with the updated guessed letters, instead it is assuming that the whole function is hidden and updating only the guessed letters for example:

    This is what it should RETURN!

    >>>update_view ('apple', 'a^^^^', 'p')
    'app^^'

    THIS IS WHAT IT IS RETURNING:

    >>>update_view ('apple', 'a^^^^', 'p')
    '^pp^^' <-- with 'a' hidden again...

    PLEASE HELP!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Whoot! You're the first poster I recall (other than myself) to doctest. Congratulations. So very nice to have tests that verify what your function should do. See the link at my signature to post executable python code.

    Anyway, you'll need to return the next character from the view or from the puzzle, whereas you using HIDDEN instead of view. You may have to rethink your start-up, such that the initial view is

    view = HIDDEN*len(puzzle)
    Code:
    def update_view (puzzle, view, guessed_letter):
        '''
            (str, str, str) -> str
    
            Return the view of the puzzle with each occurrence of the letter in
            the puzzle revealed. 
    
            >>> update_view ('apple', '^^^^^', 'a')
            'a^^^^'
            >>> update_view ('apple', 'a^^^^', 'p')
            'app^^'
        '''
        result = ''
        for (i,c,) in enumerate(puzzle):
            if c.islower() and (c not in guessed_letter):
                result+= view[i]
            else:
                result += c
        return result
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Whoot! You're the first poster I recall (other than myself) to doctest. Congratulations. So very nice to have tests that verify what your function should do. See the link at my signature to post executable python code.

    Anyway, you'll need to return the next character from the view or from the puzzle, whereas you using HIDDEN instead of view. You may have to rethink your start-up, such that the initial view is

    view = HIDDEN*len(puzzle)
    Code:
    def update_view (puzzle, view, guessed_letter):
        '''
            (str, str, str) -> str
    
            Return the view of the puzzle with each occurrence of the letter in
            the puzzle revealed. 
    
            >>> update_view ('apple', '^^^^^', 'a')
            'a^^^^'
            >>> update_view ('apple', 'a^^^^', 'p')
            'app^^'
        '''
        result = ''
        for (i,c,) in enumerate(puzzle):
            if c.islower() and (c not in guessed_letter):
                result+= view[i]
            else:
                result += c
        return result
    Is there a way to do this without using enumerate??? Because it isnt working, and i also set the variable for view as view = HIDDEN* len(puzzle) I tried it but it just wont work, its coming out as the updated version again, and not with the view + updated view with guessed letter((((
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You must have a fairly old version of python if it hasn't got enumerate. The main idea was to use the next character from view rather than HIDDEN. But of course, you can just go back to your
    Code:
        result = ''
        for i in range(len(puzzle)):
            correct = puzzle[i]
            wrong = view[i]
            if correct.islower() and (correct not in guessed_letter):
                result+= wrong
            else:
                result += correct
        return result
    Last edited by b49P23TIvg; October 17th, 2012 at 07:10 AM. Reason: fix bad formatting
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by b49P23TIvg
    You must have a fairly old version of python if it hasn't got enumerate. The main idea was to use the next character from view rather than HIDDEN. But of course, you can just go back to your
    Code:
        result = ''
        for i in range(len(puzzle)):
            correct = puzzle[i]
            wrong = view[i]
            if correct.islower() and (correct not in guessed_letter):
                result+= wrong
            else:
                result += correct
        return result

    OMG... You are a life saver!! Thank You soooo much

IMN logo majestic logo threadwatch logo seochat tools logo