4.1. Exercises

Nested Loop Questions

for i in range(0, 49, 10):
    print('Hurray')
    for j in range(0,8,2):
        print("This is fun!")

Making a Nested Loop

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Let’s count how many letters are contained in the dishes of a menu! We have a list named menu that contains multiples lists. We want to calculate how many character are contain in the entire menu.

Tasks:

  • Make an object named charater_count and give it a value of 0.
  • Make an outer loop that iterates over each list of the menu list.
  • Make an inner loop that iterates over each element in the nested list and add the length of it to charater_count.
  • Display the value of charater_count outside both loops.
Hint 1
  • Are you using 4 indentations for each loop?
  • Are you putting character_count = character_count + len(dish) in the inner loop?
Fully worked solution: