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 trialKushagra Shukla
Courses Plus Student 293 Pointshow to iterate using for loop
What is another way to do the iteration using for loop?
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for continent in continents:
i=0
while i<2:
print("*"+continent[i])
# Your code here
2 Answers
Kushagra Shukla
Courses Plus Student 293 Pointsnakalkucing That will print the entire list. What if i have to print only the first two elements of the list?
nakalkucing
12,964 PointsHmmm... I see. Sorry about that. Kicking myself for not paying enough attention. ;)
I'm now experimenting with the code to find a way to print off the first 2 elements of the list. :)
nakalkucing
12,964 PointsOk. After consulting a friend, we came up with this:
i=0
for continent in continents:
if i < 2:
print("* " + continent)
i += 1
nakalkucing
12,964 PointsJust being silly here, Kushagra Shukla. But, if you need the first two elements printed out in place of every other element of the list...
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
for cont in continents:
print ("* " + str(continents[0 : 2]))
Happy Coding! :)
nakalkucing
12,964 Pointsnakalkucing
12,964 PointsHi Kushagra Shukla! I really like seeing other people's different coding solutions. (Yours is a neat one.) I look forward to seeing any other ways people post here! But, here's the unimaginative ;) way I did it: