[Tutor] Sequencing

Dave Angel davea at ieee.org
Wed May 18 12:02:26 CEST 2011


On 01/-10/-28163 02:59 PM, Cindy Lee wrote:
> Hi Pyton Tutors thanks for adding me,
>
> I am new to Python and missed one of my classes and am not sure of my homework. We are currently on sequencing and are being asked to make a function that receives text as an argument and returns the same text, but with 1 added to each number. So far I have:
>
>
> def ReceiveAndReturn():
>
>      sentence=raw_input("Give me a sentence with variables in it: ")
>
> print ReceiveAndReturn
>
>
>
> could anyone give me any hints on what else needs to be added?

As for your code so far, you've omitted the parentheses in the call to 
ReceiveAndReturn.



That's not a very complete assignment description.  Without some example 
text, I can only guess what these "sentences" might be permitted to look 
like.  So let me make a wild guess and see where it leads us.

Suppose you assume that the numbers in the "sentence" will be unsigned 
(positive) integers, and that they will be separated from surrounding 
characters by whitespace.  That's a restrictive assumption, since a 
sentence might end with a number, and therefore there might be a period, 
not a space after it.  Similarly, a list of numbers might have commas, 
etc.   Assume also that extra spaces are irrelevant, so that one space 
between each word is fine.

So you're looking to parse a 'sentence' like:

    Joe had 44 baskets and 3 of them were broken.

What you might want to do is split that string by whitespace, then loop 
through the resulting list, discovering any tokens that start with a 
digit.  If it starts with a digit, convert it to an int, add one, and 
convert it back to a string.

Then join the tokens (strings) back together into a single string (using 
a space character), and print it out.

In my description I deliberately used several python keywords and 
library function names, so that you might be able to search them out in 
the python docs, to see how to apply them.

DaveA


More information about the Tutor mailing list