The range() method only accepts integer values as a parameter. If you try to use the range()
method with a string value, you’ll encounter the “TypeError: ‘str’ object cannot be interpreted as an integer” error.
This guide discusses why you may encounter this error and what it means. We’ll walk through an example scenario to help you understand this problem and fix it in your program.
TypeError: ‘str’ object cannot be interpreted as an integer
The range()
method creates a list of values in a particular range. It is commonly used with a for loop to run a certain number of iterations.
The method only accepts integer values as arguments. This is because the values that range()
creates are integers. Consider the following program:
create_range = range(5, 10) for n in create_range: print(n)
Our program prints out five integers to the console:
5 6 7 8 9
If range()
accepted strings, it would be more difficult for the function to determine what range of numbers should be created. That’s why you always need to specify integers as arguments. In the above example, 5 and 10 were our arguments.
An Example Scenario
We’re going to build a program that lets a pizza restaurant view the names and prices of the most popular pizzas on their menu. To start, define two lists that store the information with which our program will work:
names = ["Pepperoni", "Margherita", "Ham and Pineapple", "Brie and Chive"] prices = [9.00, 8.40, 8.40, 9.50]
These lists are parallel and appear in order of how popular a pizza is. That means the values at a particular position in the list correspond with each other. For instance, the price of a “Brie and Chive” pizza is $9.50.
Our next task is to ask the user how many pizzas they would like to display. We can do this using an input() statement:
to_display = input("Enter the number of pizzas you would like to display: ")
Now we know the number of pizzas about which the user is seeking information, we can use a for loop to print each pizza and its price to the console:
for p in range(0, to_display): print("{} costs ${}.".format(names[p], prices[p]))
Our program prints out a message containing the name of each pizza and the price of that pizza. Our loop stops when it has iterated through the number of pizzas a user has requested to display to the console.
Let’s run our program and see if it works:
Enter the number of pizzas you would like to display: 2 Traceback (most recent call last): File "main.py", line 6, in <module> for p in range(0, to_display): TypeError: 'str' object cannot be interpreted as an integer
Our program returns an error on the line of code where we define our for loop.
The Solution
We have created a for loop that runs a particular set of times depending on the value the user inserted into the console.
The problem with our code is that input()
returns a string, not an integer. This means our range()
statement is trying to create a range of values using a string, which is not allowed. This is what our code evaluated in our program:
for p in range(0, "2"): print("{} costs ${}.".format(names[p], prices[p]))
To fix this error, we must convert “to_display” to an integer. We can do this using the int()
method:
for p in range(0, int(to_display)): print("{} costs ${}.".format(names[p], prices[p]))
“to_display” is now an integer. This means we can use the value to create a range of numbers. Let’s run our code:
Enter the number of pizzas you would like to display: 2 Pepperoni costs $9.0. Margherita costs $8.4.
Our code successfully prints out the information that we requested. We can see the names of the two most popular pizzas and the prices of those pizzas.
Conclusion
The “TypeError: ‘str’ object cannot be interpreted as an integer” error is raised when you pass a string as an argument into a range()
statement. To fix this error, make sure all the values you use in a range()
statement are integers.
Now you have the knowledge you need to fix this error like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.