Does this look right? Still a little confused.... if char.isdigit(): num = int(char) + 1 a_string[index] = str(num) print a_string
________________________________ From: Marc Tompkins <marc.tompk...@gmail.com> To: Ken Baclig <kbac...@yahoo.com> Cc: "tutor@python.org" <tutor@python.org> Sent: Wednesday, July 20, 2011 3:45 PM Subject: Re: [Tutor] Homework problem On Wed, Jul 20, 2011 at 2:54 PM, Ken Baclig <kbac...@yahoo.com> wrote: Hi, > > >I'm trying to make a function that receives text (a string) as an argument and >returns the same text (as string), but with 1 added to each word that is a >number. > > >I need help getting started. > > >So far, I have: > > >def FindNumbers(a_string): > > > for index, char in enumerate(a_string): > if char.isdigit(): >a_string[index] = > > > > >def Test(): > > > sometext = "I got 432 when I counted, but Jim got 433 which is a lot >foronly 6 cats, or were there 12 cats?" > > FindNumbers(sometext) > > >Test() First of all, don't enumerate() the string; split() it instead - this will give you a list of words instead of characters. Then, look at each item in that list; check to see whether it's numeric - isdigit() works for this. If it _is_ numeric, convert it to an int, add one to it, and turn it back into a string. Join the list back into a string, and you're done. Note: you can step through the items in a list by saying (for example) "for word in words:" - but if you do it that way you can't modify any of the items. If you need to modify them - by adding 1, for example - you need to refer to them by index instead, and the quickest way to do that is "for x in range(len(words)): print words[x]". That was a bunch of broad hints - if you need help putting them together, feel free to ask.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor