Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Print a rectangle of stars with 4 rows and 7 columns. Write it two ways: with one loop and one multiplication, and with two loops.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. One row is "*" * 7. Repeat it 4 times.
One loop.
Two loops. The inner loop prints one star at a time, with end="", and the plain print() starts a new line.
Check. Both versions show the same four lines of seven stars. The inner body runs 4 times 7, which is 28 times.
Answer: The two programs above. The first is shorter, and the second shows how nested loops work.
Log in to join the discussion.
Loading…