[Tutor] Character Buffer Object Error
bgailer at gmail.com
Thu Feb 9 23:43:23 CET 2012
Always reply-all so a copy goes to the tutor list.
On 2/8/2012 11:04 PM, Michael Lewis wrote:
> Thanks Bob,
Thanks for what if you did not follow my suggestions?
Your code is still pretty buggy.
Please test it by running it, seeing that the result is not correct,
then try the desk checking.
> The below code is what I came up with without using your suggestion.
> On a scale, how novice is mine compared to what you offered? I am
> curious because I want to know if I should have come up with your
> solution instead of what I did come up with.
> def AlterInput(user_input):
> '''search for nums in str and increment/append, return new string'''
> new_output = 
> for num in user_input:
> if not num.isdigit():
> new_num = int(num)
> new_num += 1
> return ' '.join(new_output)
> def GetUserInput():
> '''Get a string from the user and pass it'''
> user_input = '''I * got 432 when I counted, but Jim got 433 which
> is a lot for only 6 cats, or were there 12 cats?'''
> return AlterInput(user_input.split())
> Your code:
> return ' '.join(str(int(num)+1) if num.isdigit() else num for num in
> On Wed, Feb 8, 2012 at 6:04 AM, bob gailer <bgailer at gmail.com
> <mailto:bgailer at gmail.com>> wrote:
> On 2/8/2012 12:56 AM, Michael Lewis wrote:
> I want to find all digits in a string and then increment those
> digits by 1 and then return the same string with the
> incremented digits.
> You got lots of good advice from others and some not-so-good advice.
> Michael said:
> 2. The following line appears wrong.
> new_output = ' '.join(user_input)
> He did not say why!
> I add:
> Master the art of "Desk Checking".
> Take pencil & paper.
> Write down the input and output of each statement as though you
> were the computer.
> Also learn to read the & understand the Python Manuals:
> str.replace(old, new[, count])
> Return a copy of the string with all occurrences of substring old
> replaced by new. If the optional argument count is given, only the
> first count occurrences are replaced.
> Notice "return a copy". It does NOT say that this will convert old
> in place.
> No one else caught this problem!
> Since this is homework we we probably should not be offering
> alternative solutions.
> However I can't resist offering the one-line solution:
> ''.join(str(int(x)+1) if x.isdigit() else x for x in 'user_input)
> Bob Gailer
> 919-636-4239 <tel:919-636-4239>
> Chapel Hill NC
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor