[Tutor] basic problem
Alan Gauld
alan.gauld at btinternet.com
Fri Oct 7 01:59:02 CEST 2011
On 06/10/11 23:11, ADRIAN KELLY wrote:
> can someone spell out to me in (simply if possible) what this programme
> is doing.
Mac has done that pretty well.
However, let me add a couple of comments:
> # this programme will adding something to everything the user
> #types by using a for loop
This comment does not describe what the program actually does.
In fact the program simply echoes what the user types in.
Well. Ok I suppose it adds a space at the front, but it goes
about it in a very long winded way!
> #set values
> new_message=" "
> VOWELS="AEIOU"
> message=raw_input ("Enter a message: ")
> for letter in message:
> if letter.lower() not in VOWELS:
> new_message = new_message+letter
> print new_message
In Python = is not a test or statement as it is in math.
For that we use ==. A single = sign is an assignment.
So
x = 42 means that x takes on the value 42.
x = 42 + 1 means x takes on the value 42+1, or 43
(Some other languages use another symbol for this which in my opinion is
better, but sadly Python doesn't )
So
new_message = new_message+letter
means new_message becomes the old value of new_message plus the value of
letter(which changes on each cycle through the for loop)
This is such a common idiom that, as Mac said, Python provides a
shorthand way of expressing it:
new_message += letter
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list