[Tutor] List Python Question..Please help

Amit Saha amitsaha.in at gmail.com
Sat Sep 28 04:11:06 CEST 2013


Hi Jacqueline,

On Sat, Sep 28, 2013 at 3:04 AM, Jacqueline Canales
<jackiexxduh3 at gmail.com> wrote:
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> x = 'Antheil'
> s = 'Saint-Saens'
> h = 'Beethoven'
> y = 'Easdale'
> k = 'Nielsen'
>
> if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
>     if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
>         if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
>             print(s,k,y)
>         else:
>             print(" ")
>
> ####Answer i Got Below
>>>>
> Saint-Saens Nielsen Easdale
>>>>
>
> Is this what i was going for in the direction i was a bit confused if we
> were suppose create loops or if statements that are verified in the actual
> composers list. I don't know i feel as if i know what i need to do i just
> cant put it together.

Nothing to worry. Let us break the problem down.

In your first post, you mentioned this for loop:

composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
for person in composers:
    print(person)

What does this do? It prints all the composers' names. However, what
you want is to print *only* the composer whose name starts and ends
with the first letter. So, what can you do?

I shall try to explain with an example from every day life. Let us
say, the management in your local cinema theatre says that you can
choose to see all of the films playing there. So, you can see all of:
'Turbo', 'Planes' and 'The Smurfs 2'. Now, let is say that your cinema
management became a little shrewd and tells you that you can only
watch the films starting with 'P'. So what do you do? In your mind,
you think which of 'Turbo',' Planes' and 'The Smurfs 2' starts with
'P'. So, you first check, 'Turbo' and you see that it fails the
condition, but 'Planes' agree with the condition and 'The Smurfs 2'
also fails. Thus, you choose 'Planes'.

So, your above program is in the right direction. What you have to now
do is, before printing the 'person', you need to check if the person's
name starts and ends with the same letter. I already showed you how
you can do so.

You may find the lower() method helpful here. It returns you a capital
letter into a lower one:

>>> 'A'.lower()
'a'

Does that make it easier?

Good luck.
-Amit


-- 
http://echorand.me


More information about the Tutor mailing list