[Tutor] Homework problem

Alan Gauld alan.gauld at btinternet.com
Wed Jul 20 01:14:31 CEST 2011


Ken Baclig wrote:
> Does this look right?  Still a little confused....
> 

Nope.
Notice that Marc said NOT to operate on characters but to split() the 
string into a wordlist. Then test for each word in the wordlist to see 
if it isdigit(). Igf so then convert the word to an int() and add one.
Then convert the new int back to a str()and insert back into your 
wordlist. Finally join() your wordlist with spaces to get your
original "sentence" back.


>         if char.isdigit():
>            num = int(char) + 1
>            a_string[index] = str(num)
> print a_string

> 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]".

Or use the enumerate() function you started with....

HTH,

Alan G.



More information about the Tutor mailing list