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.
Start your free trialCarl Larsen
1,061 PointsDefining 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.
Thanks in advance, Carl Larsen
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continents[x][0] == "A":
print(continents[x])
4 Answers
Grigorij Schleifer
10,365 PointsHi 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?
Carl Larsen
1,061 PointsHey Grigorij, I just tried it again, fixing the space in the print function and it worked! Thanks again for the help and support, Carl
Grigorij Schleifer
10,365 PointsHey Carl, that's great! I am glad I could help
Carl Larsen
1,061 PointsHi 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
Grigorij Schleifer
10,365 PointsHi 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 :)
Carl Larsen
1,061 PointsHi 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:
'* Asia' not found in '*Asia\n*Africa\n*Antarctica\n*Australia' : Hmm...not finding the correct items in your output
Im not sure what this means, but testing it a little bit; I think the first [] refers to the string # in the list and a second [] would refer to the letter # in that specific string. I think the error means that there is no string "A" and therefore nothing is printe. Don't know what to do.
Yes that game was pretty good, I'm surprised that Mexico won though.
Thanks, Carl
Grigorij Schleifer
10,365 PointsHey Carl, I will post a detailed explanation when I am back home. But I see you miss a blank space after * in the print statement. The interpreter is very picky if you donβt follow the challenge instructions :)