Sửa lỗi value is out of range 0 maya 2023 năm 2024
Our global network of 1.4 million neighbors, friends, and leaders volunteer their skills and resources to solve issues and address community needs. Show Take Action With Us 1.4 million We connect people 1.4 million members worldwide 47 million We transform communities Approximately 47 million volunteer hours each year $333 million We fund sustainable projects $333 million awarded for global service initiatives in 2020-21 News & FeaturesHelp us create lasting change Solving some of the world’s most complex and pressing problems takes real commitment and vision. Rotary members believe that we share a responsibility to take action to improve our communities. Join us, so that we can make an even bigger impact – together. Get involved No challenge is too big for usFor more than a century, we’ve bridged cultures and connected continents. We champion peace, fight illiteracy and poverty, help people get access to clean water and sanitation, and fight disease. Our newest cause is to protect our planet and its resources. Your trusted source for Minnesota news today. Read articles, view photos, or watch videos about news in Minneapolis, St. Paul, Twin Cities Metro areas, St. Cloud, Rochester, and beyond. The Star Tribune is committed to provide more of what matters to Minnesotans. All day. Every day. Your trusted source for Minnesota news today. Read articles, view photos, or watch videos about news in Minneapolis, St. Paul, Twin Cities Metro areas, St. Cloud, Rochester, and beyond. The Star Tribune is committed to provide more of what matters to Minnesotans. All day. Every day. This means that the first item in a list has an index of 0, the second item has an index of 1, and so on. You can use the index number to access the individual item. To access an item in a list using its index number, first write the name of the list. Then, inside square brackets, include the intiger that corresponds with the item's index number. Taking the example from earlier, this is how you would access each item inside the list using its index number:
You can also use negative indexing to access items inside lists in Python. To access the last item, you use the index value of -1. To acces the second to last item, you use the index value of -2. Here is how you would access each item inside a list using negative indexing:
Why does the
0 error occur in Python? Using an index number that is out of the range of the listYou'll get the
0 error when you try and access an item using a value that is out of the index range of the list and does not exist. This is quite common when you try to access the last item of a list, or the first one if you're using negative indexing. Let's go back to the list we've used so far.
Say I want to access the last item, "Lenny", and try to do so by using the following code:
Generally, the index range of a list is
2, with
3 being the total number of values in the list. With the total values of the list above being
4, the index range is
5. Now, let's try to access an item using negative indexing. Say I want to access the first item in the list, "Kelly", by using negative indexing.
When using negative indexing, the index range of a list is
6, where
7 the total number of items contained in the list. With the total number of items in the list being
4, the index range is
9. Using the wrong value in the
0 function in a Python
1 loop You'll get the
0 error when iterating through a list and trying to access an item that doesn't exist. One common instance where this can occur is when you use the wrong integer in Python's
0 function. The
0 function typically takes in one integer number, which indicates where the counting will stop. For example,
5 indicates that the counting will start from
6 and end at
4. So, by default, the counting starts at position
6, is incremented by
9 each time, and the number is up to – but not including – the position where the counting will stop. Let's take the following example:
Here, the list
6 has four values. I wanted to loop through the list and print out each value. When I used
5 I was telling the Python interpreter to print the values that are at the positions
2. However, there is no item in position 4. You can see this by first printing out the number of the position and then the value at that position.
You see that at position
6 is "Kelly", at position
9 is "Nelly", at position
5 is "Jimmy" and at position
6 is "Lenny". When it comes to position four, which was specified with
5 which indicates positions of
2, there is nothing to print out and therefore the interpreter throws an error. One way to fix this is to lower the integer in
0:
Another way to fix this when using a
1 loop is to pass the length of the list as an argument to the
0 function. You do this by using the
8 built-in Python function, as shown in an earlier section:
0 When passing
8 as an argument to
0, make sure that you don't make the following mistake:
1 After running the code, you'll again get an
5 error:
2 ConclusionHopefully this article gave you some insight into why the
5 error occurs and some ways you can avoid it. If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way. You'll also build five projects at the end to put into practice and help reinforce what you learned. Thanks for reading and happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |