Computer Science
Consider the following code segment:
a = input()
b = int(input())
c = a + b
print(c)
When the program is run, the user first enters 10 and then 5, it gives an error. Find the error, its reason and correct it
Python Data Handling
38 Likes
Answer
The error is:
TypeError: can only concatenate str (not "int") to str
It occurs because a is of type string but b is of type int. We are trying to add together a string operand and an int operand using addition operator. This is not allowed in Python hence this error occurs.
To correct it, we need to cast a to int like this:
a = int(input())
Answered By
13 Likes
Related Questions
Make change in the expression for z in the Python code given below so that the output produced is zero. You cannot change the operators and order of variables. (Hint. Use a function around a sub-expression)
x, y = 4, 8 z = x/y*y print(z)
Consider the following code segment:
a = input("Enter the value of a:") b = input("Enter the value of b:") print(a + b)
If the user runs the program and enters 11 for a and 9 for b then what will the above code display?
Consider the following expression:
x = "and" * (3 + 2) > "or" + "4"
What is the data type of value that is computed by this expression?
Find out the error and the reason for the error in the following code. Also, give the corrected code.
a, b = "5.0", "10.0" x = float(a/b) print(x)