-2

I'm trying to figure out why the output grabs the first letter of each item in the list instead of just grabbing the first element (buffalo wings)? Here's what my code looks like.

faveFoods = ['buffalo wings' , 'menudo' , 'mashed potatoes' , 'man and cheese']

for food in faveFoods:
   print('I love', food[0], 'because they are spicy')

This outputs:

I love b because it is spicy
I love m because it is spicy
I love m because it is spicy
I love m because it is spicy
6
  • If you print food inside your loop (in a separate line), you will see why it only prints the first letter. (Spoiler: because you are asking Python to.)
    – Jongware
    Mar 9, 2020 at 23:49
  • 1
    What is the issue, exactly? Have you done any debugging? I strongly recommend reading the following article: ericlippert.com/2014/03/05/how-to-debug-small-programs.
    – AMC
    Mar 10, 2020 at 1:13
  • @AMC I answered my question below with a code that outputs what I was looking for, however, it isn't in the manner that they were looking for. I will look over your link. Thanks! Mar 10, 2020 at 1:47
  • @GabeStrenk What do you mean by it isn’t in the manner that they were looking for ?
    – AMC
    Mar 10, 2020 at 2:00
  • The lesson plan I'm following is on next.tech and they have a particular code pattern that they're looking for. faveFoods\s*=\s*[([\'|\"][A-Z,a-z]*[\'|\"],\s*)*[\'|\"][A-Z,a-z]*[\'|\"]](\n| )*for\sfood\sin\s*faveFoods:\n*( \n|\t)*( |\t)print([\'|\"][A-Z,a-z,\'\"\,\!\? \!\@\$\%\&\>1-9]*) Mar 10, 2020 at 2:50

3 Answers 3

1

The for loop iterates over the faveFoods list - this means that each time the content of the for loop runs, the value of food is the actual food - and not the original list.

faveFoods = ['buffalo wings' , 'menudo' , 'mashed potatoes' , 'man and cheese']

for food in faveFoods:
    print('I love', food[0], 'because they are spicy')
    # food is 'buffalo wings' the first time, then 'menudo', etc.
    # using [0] on that value refers to the first letter in the name instead.

Instead, you probably either want to reference the element in the original list or food directly:

for food in faveFoods:
    print('I love', food, 'because they are spicy')

This will print out each element from your faveFoods list with the text in front and after.

However, since mashed potatoes usually isn't spicy, you might want to just reference the first element of your faveFoods list. In that case you don't need the for loop, and instead reference the faveFoods list directly:

print('I love', faveFoods[0], 'because they are spicy')

From the comment below - if you want to attach a reason, you can use a dictionary instead to keep the reason together with each food option:

foods = {'buffalo wings': 'because they are spicy', 
         'menudo': 'because it is a good hangover cure',
         ...
}

for food in foods:
    print('I love', food, foods[food])
    # foods[food] refers to the reason (i.e. look up <food> in the dictionary
    # given above and return the reason attached.
2
  • Thankyou. The reason I was trying to use the for loop was because for every iteration I will give a different explanation for why I like that particular item. Mar 10, 2020 at 0:03
  • See my update for how you can do this as a single structure instead
    – MatsLindh
    Mar 10, 2020 at 8:39
0

From the question, it's clear that you don't know how the for-each statement works in python. Try to refresh your knowledge, it will save you from a lot of headaches

You can google "Python for-each statement" or use Official

faveFoods = ['buffalo wings' , 'menudo' , 'mashed potatoes' , 'man and cheese']
for food in faveFoods:
    print('I love {} because they are spicy'.format(food))

If you want to output "I love buffalo wings because they are spicy" you don't need a loop for that

print("I love {} because they are spicy".format(faveFoods[0]))
0

This gave me the output I was looking for, I'm sure this quite messy and inefficient. Thanks everyone for taking the time to respond.

faveFoods = ['buffalo wings', 'menudo', 'mashed potatoes', 'mac and cheese']

for food in range(0,len(faveFoods),4):
   print('I love', faveFoods[food], 'because they are spicy')
for food in range(1,len(faveFoods),4):
   print('I love', faveFoods[food], 'because it is a good hangover cure')
for food in range(2,len(faveFoods),4):
    print('I love', faveFoods[food], 'when there is gravy')
for food in range(3,len(faveFoods),4):
    print('I love', faveFoods[food], 'when the noodles are squishy')
1
  • There is absolutely no reason to use a for loop in either of those cases. Just use the index directly: faveFoods[0], faveFoods[1], faveFoods[2], etc.
    – MatsLindh
    Mar 10, 2020 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.