CBSE Class 11 Informatics Practices Question 84 of 102

Python Programming Fundamentals — Question 45

Back to all questions
45
Question

Question 32

Find errors in the following code fragment.

cl = input("Enter your class")
print("Last year you were in class", cl - 1)
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)