[Tutor] Character Buffer Object Error

Michael Lewis mjolewis at gmail.com
Thu Feb 9 23:59:07 CET 2012

On Thu, Feb 9, 2012 at 2:43 PM, bob gailer <bgailer at gmail.com> wrote:

> 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?
I partially followed your suggestions by getting rid of str.replace(old,
new[, count]) and new_output = ' '.join(user_input). But I also wanted to
try to get this to work without simply copying your code.

> Your code is still pretty buggy.
       I don't think I made my intentions clear. I don't wont to increment
each digit. I want to find each number (for example, if the str contains
436, I don't want to interpret that as 4, 3, 6 - I instead want to
interpret that as 436, which is why I created a list and then joined back
into a string afterwards.

> Please test it by running it, seeing that the result is not correct, then
> try the desk checking.
> I don't see how this is not correct, because my input was

>  I  got 432 when I counted, but Jim got 433 which
> is a lot for only 6 cats, or were there 12 cats?

And my output was:

I  got 433 when I counted, but Jim got 434 which
is a lot for only 7 cats, or were there 13 cats?

>    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_output.append(num)
>         else:
>             num.isdigit()
>             new_num = int(num)
>             new_num += 1
>             new_output.append(str(new_num))
>     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
> user_input)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120209/f35e5abf/attachment-0001.html>

More information about the Tutor mailing list