Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Defining x inside an if statement inside a function

I am just a little confused as to how to answer this question. How can I define x as an integer going from 0 to 6 each time the code repeats? Would I need to implement a loop of some kind, or am I on the complete wrong track with this exercise? Hope you can help.

continents.py
```continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continents[x][0] == "A":
print(continents[x])
```

Hi Carl, you are very close and your logic is correct.

For this challenge, it's important that you know how to access characters in a string by index. The first character of a string is at index 0!

```string = "Africa"
print(string[0])  # will return A
```

So, you can use the same logic inside the for loop and print the list item if continent contains an A on index 0. You dont need x here, just index continent inside the if statement and use == to see if it contains an A on position 0. You will need to modify your print statement also, to get a result like this:

```* Asia
* Africa
...
...
```

Does this help?

Hey Grigorij, I just tried it again, fixing the space in the print function and it worked! Thanks again for the help and support, Carl

Hi Grigoij,

I have just modified the print statement -> print("* " + continents[x])

However; I am still extremely confused how to "index continent inside the if statement". I understand how to refer to each string (i.e. 0,1,2,3,4,5,6)) but I have no idea how to ask the program to repeat the test for each string. Could you please go into more detail regarding this concept.

Thanks again, Carl

Hi Carl, sorry for the delay ... watching Germany against Mexico .... crazy soccer game :)

I totally can understand your confusion. This topic is not trivial.

See the comments in the code that should do the trick:

```# Every item from the continents list wil be stored in continent one by one
for continent in continents:
# the first list item Asia wil be assigned to continent during the first for loop run
# inside the if statement you use indexing to see if continent starts with A at position 0
# if yes, the if condition is met (True) and the print method will print the continent
if continent[0] == "A":
print("* " + continent)
```

Does this help? Please don´t hesitate to ask again if anything is not absolutely clear ....

OMG: Goal for Mexico :)

Hi Grigorij, I tried running the code you showed above;

continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia'] for continent in continents: if continent[0] == "A": print("*" + continent)

but it presents an AssertionError: