KnowledgeBoat Logo
|

Informatics Practices

Find errors in the following code fragment.

cl = input("Enter your class")
print("Last year you were in class", cl - 1)

Python Funda

3 Likes

Answer

The error in the above code fragment occurs when trying to perform arithmetic operation on the result of the input() function. The input() function returns a string, even if the user enters a number. Therefore, attempting to subtract 1 from cl (which is a string) will raise a TypeError. The corrected code is:

cl = int(input("Enter your class: "))
print("Last year you were in class", cl - 1)

Answered By

3 Likes


Related Questions