[Tutor] Character Buffer Object Error

Alan Gauld alan.gauld at btinternet.com
Wed Feb 8 10:12:06 CET 2012


On 08/02/12 05:56, Michael Lewis wrote:

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

Christian answered the reason for the error.
Here are some other comments...

> def AlterInput(user_input):
>      print user_input
>      new_output = ''

make this a list not a string

>      for index, char in enumerate(user_input):

you don;t need index or enumerate just a simple for loop.

>          if char.isdigit():
>              new_char = int(char)
>              new_char += 1

How will you handle 9? Insert 10 or lose the 1 (or the zero?)
Your choice... I'll assume you insert 10

>              new_output = ' '.join(user_input)

I gave no idea what this is for, why insert spaces?
Instead append new_char as a str to the list
new_output.append(str(new_char))

This needs to be outside the if statement because
you want to add all the chars. You will need to play with the 
assignments to make that work but it should be a lot simpler...

At the very end join the list back to a string:

return ''.join(new_output)

>              new_output.replace(char, new_char)

You don't need this with a list

>      print new_output

Best to keep print statements outside the function and just return the value


>      AlterInput(user_input.split())

You don't need to split, just pass in the users string.

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list