. Advertisement .
..3..
. Advertisement .
..4..
Here is the program I run:
x = input("Give starting number: ")
y = input("Give ending number: ")
for i in range(x,y):
print(i)
Give starting number: 4
Give ending number: 13
4
5
6
7
8
9
10
11
12
13
After I run, it returns an error:
Traceback (most recent call last):
File "C:/Python33/harj4.py", line 6, in <module>
for i in range(x,y):
TypeError: 'str' object cannot be interpreted as an integer
Does anyone have any suggestions for the problem below: typeerror: ‘str’ object cannot be interpreted as an integer in the python. How to correct it?
The cause:
This error happens due to range() only accepts int values as parameters.
Solution:
Using int() to change your inputs is a nice solution to resolve this error:
Outputs as below:
The simplest solution would be:
input
returns a string ( in Python 2 ).int
attempts to convert it into an integer. If the string isn’t valid, this code will throw an error. You might want to modify it usingtry
/except
statements.