FAQ: Learn Python- Loops - Create your own

This community-built FAQ covers the “Create your own” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Create your own:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

2 posts were split to a new topic: Why is This Code Not Passing?

I have created the following code, but I want it to do something else:

shopping_list1 = ['apple', 'kale', 'chicken', 'salmon']
shopping_list2 = ['banana', 'eggplant', 'snacks', 'wholewheat bread']

for a,b in zip(shopping_list1, shopping_list2):
  if a == 'snacks':
    print a, " is not a very good choice!"
  elif b == 'snacks':
    print b, " is not a very good choice!"
  else:
    print "Buy " + str(a) + " and " + str(b)

I want it to go over both lists, take out the bad choices and print out a combined shopping list with the healthy options. Can someone help me do that? I tried:

if a == 'snacks':
    print a, " is not a very good choice!"
    shopping_list1 -= a
  elif b == 'snacks':
    print b, " is not a very good choice!"
   print a, " is not a very good choice!"
    shopping_list2 -= b

okay, but this:

shopping_list1 -= a

won’t work, how would python know what subtracting a string from a list is suppose to mean? Removing from the list you are looping over isn’t a good idea anyway, has all kind of unintended consequences.

its better to create new list(s) with the results you want.

1 Like

Some food for thought (not lesson solution)…

>>> shopping_list1 = ['apple', 'kale', 'chicken', 'salmon']
>>> shopping_list2 = ['banana', 'eggplant', 'snacks', 'wholewheat bread']
>>> healthy_food_list = []
>>> unhealthy_food_list = ['snacks']
>>> for a, b in zip(shopping_list1, shopping_list2):
    c = 0
    if a not in unhealthy_food_list: healthy_food_list.append(a)
    else: c += 1; d = a
    if b not in unhealthy_food_list: healthy_food_list.append(b)
    else: c += 1; d = b
    if c: print (d + ' is not a very good choice!')

    
snacks is not a very good choice!
>>> print ('Buy these items: \n    ' + ',\n    '.join(healthy_food_list))
Buy these items: 
    apple,
    banana,
    kale,
    eggplant,
    chicken,
    salmon,
    wholewheat bread
>>> 

Thanks for both of the replies. I can definitely work with this and solve my problem.

1 Like

for non native english speakers, the instructions are confusing. can it be rephrase like this?

Build your for/else statement in the editor. Execution of the else branch is optional, but your code should print something (at least).

How could I get this to print the the string variable correctly?

st = "This is a string"
for s in st:
    if s.isalpha():
        print(s),
    else:
        print(" "),
else:
    print('\nThis is the end of the string')
T h i s   i s   a   s t r i n g 
This is the end of the string

In Python 3 it would be

st = "This is a string"
for s in st:
    if s.isalpha():
        print(s, end="")
    else:
        print(" ", end="")
else:
    print('\nThis is the end of the string')
This is a string
This is the end of the string

in python2, print is statement, so then it would be:

print s,

the comma means print on the same line. If i remember correctly, python2 has reached EoL so not much longer relevant to remember.

1 Like

Why is it not looping through the whole list till after the question?

nuts = ['peanuts','cashews', 'almonds', 'pecans']

for index, n in enumerate(nuts):
  print index + 1, n
  what_nut = raw_input('What kind of nuts do you have?')
   if what_nut == "peanuts":
      print "sorry I'm allergic"
   break
else:
  print "I'd Love some"

Unmatched indent. One suspects you do want to break at this point. The indentation should match that of the line above it. It is the price of mixing tabs and spaces. Make them both the same and this should be okay.

characters = [ ‘obedient’, ‘loyal’, ‘neat’, ‘disrespectful’]
print ‘A Child is…’
for c in characters:
if c == ‘disrespectful’:
print ‘And not disrespectful’
print ‘A’,c
else:
print ‘that is all’
why is it not printing my list and prints the else loop thrice