Informatics Practices

Find out the errors in the following code snippet and rewrite the code by underlining the corrections made.

30=y
for i in range (2, 6)
    print (true)
else:
print ("Loop over")

Python Control Flow

4 Likes

Answer

30=y #Error 1
for i in range (2, 6) #Error 2
    print(true) #Error 3
else:
print ("Loop over") #Error 4

Error 1 — This is an invalid assignment statement. The variable should be on the left side.
Error 2 — The for loop header is missing a colon at the end.
Error 3 — 'true' should be 'True' to be a valid boolean value in Python.
Error 4 — The else block is not properly indented.

The corrected code is:

y = 30
for i in range(2, 6):
    print(True)
else:
    print("Loop over")

Answered By

2 Likes


Related Questions