212k views
5 votes
The code below assigns the 5th letter of each word in food to the new list fifth. However, the code currently produces errors. Insert a try/except clause that will allow the code to run and produce of list of the 5th letter in each word. If the word is not long enough, it should not print anything out. Note: The pass statement is a null operation; nothing will happen when it executes.

food = ["chocolate", "chicken", "corn
fifth = []
for x in food:
fifth.append(x[4])

User JKoplo
by
6.4k points

1 Answer

2 votes

Answer:

Answered below

Step-by-step explanation:

foods = ["chocolate", "chicken", "corn"]

fifth_char = []

for food in foods:

"""Add a try statement to add fifth character to fifth_char list. If word is not long enough, the except part executes the the pass and nothing is printed.""'

try:

fifth_char.append(food[4])

except:

pass

#print out elements in new list

for c in fifth_char:

print(c)

User Mark Segal
by
6.5k points