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
"""
Latest PCEP-30-02 Practice Questions with 124 Q&As
Updated Study Material | Instant Download | Detailed Answers and Explanations
Subscribe
Login
0 Comments