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 October 16th, 2012, 06:02 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
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!

Reply With Quote
  #2  
Old October 16th, 2012, 06:58 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old October 16th, 2012, 11:06 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Quote:
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((((

Reply With Quote
  #4  
Old October 17th, 2012, 07:07 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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

Reply With Quote
  #5  
Old October 17th, 2012, 08:15 AM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need HELP with this function!

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