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
2 Likes
Related Questions
Give the output.
a, b, c = 10, 20, 30 p, q, r = c - 5, a + 3, b - 4 print(p, q, r)Find the errors in following code fragment : (The input entered is XI)
c = int (input ( "Enter your class") ) print ("Your class is", c)Write a Python program that accepts radius of a circle and prints its area.
Write Python program that accepts marks in 5 subjects and outputs total and average marks.