KnowledgeBoat Logo
|

Computer Science

Find errors (if any) in the following code and rewrite the code underlining each correction:

x = int("Enter value of x:")
for in range[0, 10]:
    if x = y
        print("They are equal") 
    else:
        Print("They are unequal")

Python Control Flow

8 Likes

Answer

x = int("Enter value of x:") #Error 1
for in range[0, 10]: #Error 2
    if x = y #Error 3
        print("They are equal") 
    else:
        Print("They are unequal") #Error 4

Error 1 — The input() function is missing from the code to prompt the user for input.

Error 2 — The syntax of the range() function uses parentheses () rather than square brackets [] and the variable y is missing in the for loop declaration (for y in range(0, 10):).

Error 3 — The comparison operator in the if statement has = (assignment) instead of == (equality comparison) and colon (:) is missing at the end.

Error 4 — The print function should be written with a lowercase 'p' for proper syntax.

The corrected code is:

x = int(input("Enter value of x:"))
for y in range(0, 10):
    if x == y:
        print("They are equal") 
    else:
        print("They are unequal")

Answered By

3 Likes


Related Questions