python – TypeError: str object cannot be interpreted as an integer

python – TypeError: str object cannot be interpreted as an integer

A simplest fix would be:

x = input(Give starting number: )
y = input(Give ending number: )

x = int(x)  # parse string into an integer
y = int(y)  # parse string into an integer

for i in range(x,y):
    print(i)

input returns you a string (raw_input in Python 2). int tries to parse it into an integer. This code will throw an exception if the string doesnt contain a valid integer string, so youd probably want to refine it a bit using try/except statements.

You are getting the error because range() only takes int values as parameters.

Try using int() to convert your inputs.

python – TypeError: str object cannot be interpreted as an integer

x = int(input(Give starting number: ))
y = int(input(Give ending number: ))

P.S. Add function int()

Leave a Reply

Your email address will not be published.