[Tutor] List Python Question..Please help
Steven D'Aprano
steve at pearwood.info
Fri Sep 27 11:52:13 CEST 2013
Hi Jacqueline, and welcome!
Further information below...
On Fri, Sep 27, 2013 at 12:48:26AM -0500, Jacqueline Canales wrote:
> Write a program that lists all the composers on the list ['Antheil',
> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and
> ends with the same letter (so Nielsen gets lsited, but Antheil doesn't).
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> for person in composers:
> print(person)
One of the secrets to programming is to write out the instructions to
solve the problem in English (or whatever your native language is), as
if you were giving instructions to some other person, telling them what
to do. Then you just need to make those instructions more and more
precise, more and more detailed, as computers are much dumber than any
person.
For instance, you might start off with:
Look at each composer's name, and print it only if it begins and ends
with the same letter.
The computer doesn't understand "look at each composer's name", but you
know how to tell the computer the same thing:
composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
for person in composers:
so you're already half-way there. "print it only if" needs to be written
the other way around:
if it begins and ends with the same letter, print it
The computer doesn't know what "it" is, so you have to spell it out
explicitly:
if person begins and ends with the same letter, print person
Change the syntax to match what the Python programming language expects:
if person begins and ends with the same letter:
print person
Now the only bit still to be solved is to work out the "begins and ends"
part. I'm not going to give you the answer to this, but I will give you
a few hints. If you copy and paste the following lines into the Python
interpreter and see what they print, they should point you in the right
direction:
name = "Beethovan"
print "The FIRST letter of his name is", name[0]
print "The LAST letter of his name is", name[-1]
print "Are the SECOND and THIRD letters equal?", name[1] == name[2]
print "Does X equal x?", 'X' == 'x'
print "Convert a string to lowercase:", name.lower()
Good luck, and don't hesitate to come back if you need any further help.
--
Steven
More information about the Tutor
mailing list