Bird
0
0

You want to write a function that prints numbers 1 to 3, each followed by 'Done' on a new line. Which code correctly uses indentation to achieve this?

hard📝 Application Q15 of 15
Python - Basics and Execution Environment
You want to write a function that prints numbers 1 to 3, each followed by 'Done' on a new line. Which code correctly uses indentation to achieve this?
def print_numbers():
    for i in range(1, 4):
        print(i)
    print('Done')
A<pre>def print_numbers(): for i in range(1, 4): print(i) print('Done')</pre>
B<pre>def print_numbers(): for i in range(1, 4): print(i) print('Done')</pre>
C<pre>def print_numbers(): for i in range(1, 4): print(i) print('Done')</pre>
D<pre>def print_numbers(): for i in range(1, 4): print(i) print('Done')</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Understand desired output

    Each number 1 to 3 should print, then 'Done' on a new line after each number.
  2. Step 2: Check indentation for print statements

    To print 'Done' after each number, both print(i) and print('Done') must be inside the for loop, indented equally.
  3. Step 3: Identify correct option

    def print_numbers():
        for i in range(1, 4):
            print(i)
            print('Done')
    has both prints indented inside the loop, so 'Done' prints after each number.
  4. Final Answer:

    def print_numbers():\n for i in range(1, 4):\n print(i)\n print('Done') -> Option C
  5. Quick Check:

    Same indentation inside loop = prints per iteration [OK]
Quick Trick: Indent all loop actions equally to repeat each time [OK]
Common Mistakes:
MISTAKES
  • Indenting 'Done' outside loop causing single print
  • Not indenting loop body at all
  • Mixing indentation levels inside loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes