[Tutor] Help

Alan Gauld alan.gauld at btinternet.com
Thu Mar 21 01:26:56 CET 2013


On 20/03/13 19:57, travis jeanfrancois wrote:
> I create a function that allows the user to a create sentence by
>   inputing  a string and to end the sentence with a  period meaning
> inputing "." .The problem is while keeps overwriting the previuos input

'While' does not do any such thing. Your code is doing that all by 
itself. What while does is repeat your code until a condition
becomes false or you explicitly break out of the loop.

> Here is my code:
>
> def B1():

Try to give your functions names that describe what they do.
B1() is meaningless, readSentence() would be better.


>   period = "."
>   # The variable period is assigned

Its normal programming practice to put the comment above
the code not after it. Also comments should indicate why you
are doing something not what you are doing - we can see that
from the code.

>   first = input("Enter the first word in your sentence ")
>   next1 = input("Enter the next word in you sentence or enter period:")

>   # I need store the value so when while overwrites next1 with the next
> input the previous input is stored and will print output when I call it
> later along with last one
>   # I believe the solution is some how implenting this expression x = x+
> variable

You could be right. Addition works for strings as well as numbers.
Although there are other (better) options but you may not have covered 
them in your class yet.

>   while  next1 != (period) :

You don;t need the parentheses around period.
Also nextWord might be a better name than next1.
Saving 3 characters of typing is not usually worthwhile.

>      next1  = input("Enter the next word in you sentence or enter period:")

Right, here you are overwriting next1. It's not the while's
fault - it is just repeating your code. It is you who are
overwriting the variable.

Notice that you are not using the first that you captured?
Maybe you should add next1 to first at some point? Then you
can safely overwrite next1 as much as you like?

>      if next1 == (period):

Again you don;t need the parentheses around period

>          next1 = next1 + period

Here, you add the period to next1 which the 'if' has
already established is now a period.

>          print ("Your sentence is:",first,next1,period)

And now you print out the first word plus next1 (= 2 periods) plus a 
period = 3 periods in total... preceded by the phrase "Your sentence 
is:" This tells us that the sample output you posted is not from this 
program... Always match the program and the output when debugging or you 
will be led seriously astray!

> PS : The" #"  is I just type so I can understand what each line does

The # is a comment marker. Comments are a very powerful tool that 
programmers use to explain to themselves and other programmers
why they have done what they have.

When trying to debug faults like this it is often worthwhile
grabbing a pen and drawing a chart of your variables and
their values after each time round the loop.
In this case it would have looked like

iteration	period	first	next1
0		.	I	am
1		.	I	a
2		.	I	novice
3		.	I	..

If you aren't sure of the values insert a print statement
and get the program to tell you, but working it out in
your head is more likely to show you the error.


HTH,

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



More information about the Tutor mailing list