Embercode
✕
Pattern PrintingQuick quiz

Pattern debugging in Python

6 questions. Covers Pattern debugging lab in Python.

Questions 1 to 5 of 6

  1. 1. This diamond with n = 3 prints its widest row twice. Which part of the program is the bug? ``python n = 3 for i in range(n): print(" " * (n - 1 - i) + "*" * (2 * i + 1)) for i in range(n - 1, -1, -1): print(" " * (n - 1 - i) + "*" * (2 * i + 1)) ``
  2. 2. What does this print? ``python n = 3 total = 0 for i in range(n): for j in range(2 * i + 1): total += 1 print(total) ``
  3. 3. A pyramid with n = 4 should have 7 stars on its bottom row, but it prints 6. The star loop is: ``python for j in range(2 * i): print("*", end="") `` Which change fixes the star count?
  4. 4. This program should print a pyramid of three rows, but it prints a fourth row. ``python n = 3 for i in range(n + 1): print(" " * (n - 1 - i) + "*") `` What causes the extra row?
  5. 5. Put the five steps of the pattern-debugging method in order.

    Use the arrows to put the steps in order.

    1. 1.Check the first and last rows by hand
    2. 2.Compare the output with the picture, one line at a time
    3. 3.Change one thing, run again and compare
    4. 4.Draw the picture you want for a small n, and count the rows
    5. 5.Print the counts for each row before the symbols

4 question(s) left on this page.