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 trialMichael Russell
1,575 PointsOnly printing out continents that begin with the letter "A". Can someone help me out?
I am on Task 2 of this challenge. I understand they want me to set up the code so that only the continents names that begin with the letter "A" are printed out (Asia, Africa, Antarctica, Australia). The "HINT" says that indexing is involved. I have tried everything I can think of regarding indexing that I've learned from the previous chapters, but I keep getting told what I'm doing is incorrect. Apparently I'm not supposed to "pop" items off the list (the program checker says that's incorrect). And no other combination of things I've used seems to work. If anyone can give me an extra hint or advice on what I'm missing, PLEASE help me. Thank you.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
print("* {}".format(continent))
4 Answers
boi
14,242 PointsHey Michael!, check this out.
for continent in continents:
if continent[0] == "A": # I used indexing here, also remember that indexing starts off with 0.
print("* " + continent)
Did this help? And most importantly, did you understand the code?
Ave Nurme
20,907 PointsHey all!
This may be a bit advanced code for beginners but this also works (please check the code below).
NOTE: I am using f-string here instead of .format
. It's the newest Python syntax to do string formatting (available since Python 3.6). You can google it and check it out. Happy coding! :-)
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
if continent.startswith('A'):
print(f'* {continent}')
"""
Output:
* Asia
* Africa
* Antarctica
* Australia
"""
Michael Russell
1,575 PointsHello Ave,
This code you've shown is a bit more advanced than what I'm currently studying (as you yourself mentioned). That being said, it's nice to know of other solutions, and get a glimpse of what I may be learning in the future. Thank you for your response.
Ave Nurme
20,907 PointsNo worries! :-) I do advise you to look into f-string as you might bump into it in your further studies here at Treehouse. I took the Python Basics course here about 3 weeks ago and at the moment taking the advanced course. F-string is actually easier to write down and also easier to read. Good luck with your studies!
Trace Harris
Python Web Development Techdegree Graduate 22,065 Points@Michael, looks like the you have the answer, and even in a couple of different ways. Awesome!! just a couple of things index [0] will always be the first value in an iterable. What Ave has done is taken a string method from Pythons standard library https://docs.python.org/3/library/I would take a look because as a python developer the standard library is invaluable. Containing methods and operations on everything from "strings" to HTTP Request and Response objects. and here is a huge bonus. It's built into the language!
Michael Russell
1,575 PointsMichael Russell
1,575 PointsThank you, Boi! Just tried it out and works perfectly. I knew the solution was something simple, but I originally ended up overthinking it. Will be going back over notes (and writing this info down in notes) to better commit it to memory. I really your help.