In Python 3, the xrange function was removed. If you try to use the xrange function in a Python 3 program, you’ll encounter the
NameError: name ‘xrange’ is not defined error.
In this guide, we’re going to discuss what this error means and why you may encounter it. We’ll discuss an example of this error so you can learn how to fix it in your code.
NameError: name ‘xrange’ is not defined
In Python 2, the xrange function lets you create a range of numbers. Consider the following code snippet:
Our code returns [0, 1, 2], which is all the numbers in the range of 0 and 3 (exclusive of 3). The
xrange() function returns a list of numbers.
Python 3 removed the
xrange() function in favor of a new function called range(). The
range() function, like
xrange(), produces a range of numbers.
There are two differences between
range()have different names.
xrange()function generates a list of numbers. The
range()function generates an object.
range() function generates an object, you have to convert it into a list if you want to view a list of numbers. Otherwise, these two methods function in the same way.
An Example Scenario
We’re going to write a program that displays the first few players who are at the top of a card game leaderboard. To start, let’s declare our leaderboard as a list:
leaderboard = ["Alex", "Jonas", "Emma", "Kate"]
Next, we’re going to ask the user how many scores they would like to view:
to_view = int(input("How many scores would you like to view? "))
We convert the value that a user inserts into an integer. This is because the
xrange() function only supports integers.
Next, let’s iterate over our leaderboard list using a for loop. We will only display the number of results that the user has requested to view on the console:
for l in xrange(0, to_view): print(l + 1, leaderboard[l])
We use the
xrange() function to generate a range of numbers between 0 and the number the user inserted. In our for loop, we use a
print() statement to print out the leaderboard entry that correlates with the number in the
xrange() list that our loop is viewing.
We print out the value of “l” plus one alongside the name of a player. This lets us see their position. Because lists are indexed from zero, we add one to “l”. This prevents the first player on our list being at position “0” and so on.
Let’s run our code and see what happens:
How many scores would you like to view? 2 Traceback (most recent call last): File "main.py", line 5, in <module> for l in xrange(0, to_view): NameError: name 'xrange' is not defined
Our program returns an error.
We’ve used the
xrange() function to generate a range of numbers. In Python 2, this would be valid. We are using Python 3 to run our program, which means we cannot reference the
To solve our error, we need to replace
xrange() with the
for l in range(0, to_view): print(l + 1, leaderboard[l])
For this use case, both
range() will return the same result. Let’s run our code to see if it works:
How many scores would you like to view? 2 1 Alex 2 Jonas
Our program successfully displays the names of the players in the tournament who appear in the first two positions on the leaderboard.
Redefining the xrange() Method
A potential solution to this problem is to define a variable called xrange at the start of your program that is equal to the
xrange = range
This is not a good solution to this error. This is because you are merely avoiding the problem of renaming existing methods. It would be confusing for someone to read through a Python 3 codebase and see
xrange() statements, even if you have defined xrange as a variable.
The best solution is to rename your
xrange() statements to
range() and to make any necessary changes to ensure your codebase works in Python 3.
The “NameError: name ‘xrange’ is not defined” error is raised when you try to use the
xrange() method to create a range of numbers in Python 3. To solve this error, update your code to use the
range() method that comes with Python 3.
range() is the Python 3 replacement for
xrange(). Now you have the know-how you need to solve this error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more