Traversing a list - Answers
Q1. Get the code working.
Q2. This program creates two lists. In then prints out how many monsters are in the monsters list and the whole list. Finally, it prints out each monster on a separate line.
Q3. In this line:
print('There are',len(monsters),"monsters in the monster's list.")
double quotes were used around "monsters in the monster's list" because we needed to use a single apostrophe in the word monster's.
Q4. To modify the program so that the monsters list is extended using the extend method and the extra_evil list and print out the list, add this code:
monsters.extend(extra_evil)
print('There are',len(monsters),"monsters in the monster's list.")
print(monsters)
Q5. To print out the total number of characters (including spaces) in each monster's name, do this:
i=0
while i<len(monsters):
print(monsters[i],end=' >>> ')
print(len(monsters[i]), 'characters long')
i += 1
Q6. To remove the third item in the list and display the monsters list again, along with the length of characters in each name and the monster that was removed from the list, add this:
print('\n')
temp = monsters.pop(2)
print('There are now',len(monsters),"monsters in the monster's list after",temp,'was removed.')
print(monsters)
i=0
while i<len(monsters):
print(monsters[i],end=' >>> ')
print(len(monsters[i]), 'characters long')
i += 1
Q7. To print out the list in reverse order, add this code:
print('\nHere is the list in reverse order ...')
j=len(monsters)
while j > 0:
print(monsters[j-1],end=' >>> ')
print(len(monsters[j-1]), 'characters long')
j -= 1
Q8 - Q10 Use the extend method and print, as done in question 4.