Skip to content Skip to sidebar Skip to footer

TypeError: 'str' Object Cannot Be Interpreted As An Integer

I don't understand what the problem is with the code, it is very simple so this is an easy one. x = input('Give starting number: ') y = input('Give ending number: ') for i in rang

Solution 1:

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 doesn't contain a valid integer string, so you'd probably want to refine it a bit using try/except statements.


Solution 2:

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

Try using int() to convert your inputs.


Solution 3:

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

P.S. Add function int()


Solution 4:

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

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

This outputs:

i have uploaded the output with the code


Solution 5:

I'm guessing you're running python3, in which input(prompt) returns a string. Try this.

x=int(input('prompt'))
y=int(input('prompt'))

Post a Comment for "TypeError: 'str' Object Cannot Be Interpreted As An Integer"