The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need HELP with this function!
Discuss Need HELP with this function! in the Python Programming forum on Dev Shed. Need HELP with this function! Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 16th, 2012, 06:02 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
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!
|

October 16th, 2012, 06:58 PM
|
 |
Contributing User
|
|
|
|
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!
|

October 16th, 2012, 11:06 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
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  ((((
|

October 17th, 2012, 07:07 AM
|
 |
Contributing User
|
|
|
|
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
|

October 17th, 2012, 08:15 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|