CBSE Class 10 Computer Applications Question 2 of 26

Python Conditionals and Loops — Question 2

Back to all questions
2
Question

Question 2

Consider the following code fragment :

n = 3
while n == 3
    print n
n = n - 1

(a) What will be the output of above code?

(b) What is the reason behind this output ?

(c) What change/corrections will you make to above code to make it print some output ?

Answer

(a) The given code will generate a syntax error.

(b) The syntax of while statement has a colon after the condition which is missing in the given code. Thus, a syntax error is generated.

(c) The following changes must be made to the code to print some output:

n = 3
while n == 3 :
    print (n)
    n = n - 1

The code given above will generate the following output:

3