Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Mohammed Saleh
seal-mask
.a{fill-rule:evenodd;}techdegree
Mohammed Saleh
Python Development Techdegree Student 2,980 Points

can't pass task 2 of continental..

Here is my code..

for continent in continents:
    print(" * " + continent)
    if continent [0,3,5,6] == "A":
        print(continent)

what is wrong. thanks in advanced

7 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,464 Points

Hey Mohammed Saleh, you’re close.

The loop variable continent is a string. You need to check the first character to se if it’s an “A”. Try:

if continent[0]...

Your current code is not syntactically correct. Since continent is a string. The only available option is a single character using [0] or other valid index; or to use a sting slice

Post back if you need more help. Good luck!!!

Mohammed Saleh
seal-mask
.a{fill-rule:evenodd;}techdegree
Mohammed Saleh
Python Development Techdegree Student 2,980 Points
for continent in continents:
    print(" * " + continent)
if continent [0] == "A":
        print(continent)

the error: Bummer: AssertionError: 'South America' unexpectedly found in '* Asia\n * South America\n * North America\n * Africa\n * Europe\n * Antarctica\n * Australia\nAustralia' : Whoops! I found a country that didn't start with A in your output

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

What you have is:

for continent in continents:
    print(" * " + continent)
if continent [0] == "A":
        print(continent)

What you need is of the form

for continent in continents:
    if ...
        print(...
Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points
for continent in continents:
    if continent[0] == "A":
        #       ^ no space before bracket
        print(" * " + continent)
Mohammed Saleh
seal-mask
.a{fill-rule:evenodd;}techdegree
Mohammed Saleh
Python Development Techdegree Student 2,980 Points

I appreciate your help..to be clear the challenge consists of two tasks, first task it requires us to print the continents with * before each one, which we can do it by ... for continent in continents: print ("* " + continent)

then it asks us to print the continents that starts with A only, without changing the code from task 1. so should I code under : for continent in continents: print ("* " + continent)

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

The code will not pass if the original code from task 1 is left in place unaltered.

In looking at this challenge I don’t see instructions regarding “without changing code from task 1”

Are you running a different challenge?

Mohammed Saleh
seal-mask
.a{fill-rule:evenodd;}techdegree
Mohammed Saleh
Python Development Techdegree Student 2,980 Points

Important: In each task of this code challenge, the code you write should be added to the code from the previous task..

anyway I altered the code and it worked, thanks so much you were really helpful.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

In this case, “added to” means “merged with”. If the code must not be changed in a subsequent task, the wording will more explicit.

Great you’ve made it through!

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

I answer many of the forum questions from my phone. The warning you quote doesn’t show up on small screens. Sorry for not seeing that.