Exam4Training

What is the expected output of the following code?

What is the expected output of the following code?

def func(text, num):

while num > 0:

print(text)

num = num – 1

func(‘Hello’, 3)
A . An infinite loop.
B. Hello
Hello
Hello
C. Hello
Hello
Hello
Hello
D. Hello
Hello

Answer: A

Explanation:

Topics: def while indentation

Try it yourself:

def func(text, num):

while num > 0:

print(text)

num = num – 1

func(‘Hello’, 3) # An infinite loop

The incrementation of num needs to be inside of the while loop.

Otherwise the condition num > 0 will never be False

It should look like this:

def func(text, num):

while num > 0:

print(text)

num = num – 1

func(‘Hello’, 3)

"""

Hello

Hello

Hello

"""

Exit mobile version