[Tutor] Character Buffer Object Error

Michael Lewis mjolewis at gmail.com
Wed Feb 8 06:56:41 CET 2012


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.

I've tried the following code, but I am getting the following error. How do
I do this properly?

def AlterInput(user_input):
    print user_input
    new_output = ''
    for index, char in enumerate(user_input):
        if char.isdigit():
            new_char = int(char)
            new_char += 1
            new_output = ' '.join(user_input)
            new_output.replace(char, new_char)
    print 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?'''
    AlterInput(user_input.split())


Traceback (most recent call last):
  File "C:/Python27/Homework/Homework 4_1.py", line 25, in <module>
    GetUserInput()
  File "C:/Python27/Homework/Homework 4_1.py", line 23, in GetUserInput
    AlterInput(user_input.split())
  File "C:/Python27/Homework/Homework 4_1.py", line 15, in AlterInput
    new_output.replace(char, new_char)
TypeError: expected a character buffer object

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120207/28c417c8/attachment.html>


More information about the Tutor mailing list