On 2012/02/08 07: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.
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.
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
new_char is of type int and not type str, so cast it back to a string
before using it in your call to .replace as it expects both arguments to
be strings.
--
Christian Witts
Python Developer
//
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor