Why Is This Going Out of Range

Why is this going out of range?

You are trying access 5th element of num_list array in the second iteration of for loop. After the first iteration num becomes 4, so program crashes when it tries to evaluate num_list[num + 1].

num variable holds the actual element in the list. It is not index to element.

To iterate over indices, you may try for num in range(len(num_list) - 1) which should solve the issue. (Note -1 in the paranthesis)

Why am I getting this Index out of range error?

First of all, don't call a list list. Give it another name.

The problem is after each time you get a letter, you delete that letter. So if the list is [1,2,3] and I get 3, 3 is deleted.

When that happens, the list now only has 2 items. Earlier, list[2] would work. Now that 3 is gone, list[2] does not exist.

Either you have to remove the delete function or you have to setup a variable that decreases each time a letter is selected and get the random with that:

import random

lista = ["a", "b", "c", "d", "e", "f", "g", "h" , "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

yn = "y"
max = 25

while yn == "y":
npat = random.randint(0, max)
print(npat)
print(lista[npat])
del lista[npat]
max = max - 1
yn = input("Again?: ")

Why am I getting this out of range error

I suggest adding some checks to your code so you don't assume there are two '-'s where you expect to find them.

if ( str.size() >= 4 && str[3] == '-' )
{
str.erase(3,1);
if ( str.size() >= 6 && str[5] == '-' )
{
str.erase(5,1);
}
else
{
std::cout << "Did not find a '-' at the 6-th position of the string.\n";
std::cout << str << std::endl;
}
}
else
{
std::cout << "Did not find a '-' at the 4-th position of the string.\n";
std::cout << str << std::endl;
}

For loop is going out of range

When a list is initialized:

New List(Of Double)()

It has no elements, it is empty. So you can't access any elements in it. But the first thing you do with your avx list is try to access an element:

avx(i) = wi(i) * hi(i)

Regardless of the value of i, this will fail because the list has no elements.

You already know how to add an element to a list, for example here you add an element to the wi list:

wi.Add(CInt(DataGridView1.Rows(i).Cells("w").Value))

If you want your avx list to have any elements, you need to add some.

Why does this list index go out of range?

I suspect this error only occurs when there are multiple mouse move events in the event queue. Normally, pygame is fast enough to render the screen while accruing no more than one new user input event, so sendCoords will only be called once in between drawCircles calls. In that case, coords never exceeds a size of 52. But if several mouse move events accrue (perhaps due to system lag, or because the user is jiggling his mouse really fast), then sendCoords may be called many more times in a row. So by the time drawCircles executes, coords can have 53 elements, or even more.

This becomes a problem when you reach drawCircles:

def drawCircles(snakeLength):
for i in range(len(coords)):
pygame.draw.circle(SCREEN, colorList[i], coords[i], abs(i-(len(coords))) * 5, 0)#### Why does the list index go out of range?
if i > snakeLength :
popList()

Let's say this function executes when coords contains 53 elements, and snakeLength is 50. The loop will iterate normally until i equals 51. Then the i > snakeLength will evaluate to True, and popList will be called. Now coords is one element smaller, and has a length of 52. That iteration of the loop ends, and the next iteration starts. i will equal 52. The pygame.draw.circle line will attempt to access coords[i], but because coords no longer has 53 elements, coords[i] will raise an IndexError trying to access the 53rd element.

Python is not smart enough to understand that the for i in range(len(coords)) loop should end one iteration earlier than usual if coords reduces in size by one. It happily iterates all the way up to the original length of the list, heedless of whether this might cause a crash.

One possible solution is to move popList outside of the list, so the size of coords doesn't change while you're iterating over it.

def drawCircles(snakeLength):
for i in range(len(coords)):
pygame.draw.circle(SCREEN, colorList[i], coords[i], abs(i-(len(coords))) * 5, 0)#### Why does the list index go out of range?

while len(coords) > snakeLength :
popList()

You might be thinking "But why is it OK to modify coords' length in this while loop, when it wasn't OK to do it in the for loop?" The critical distinction is the evaluation time of the two statements. range(len(coords)) exectues exactly once before the loop starts, so modifications to coords won't be noticed. but len(coords) > snakeLength executes at the beginning of very iteration of the while loop, so changes to coords gets noticed right away.

Why am I getting #1264 - Out of range value for column 'Identity_no' at row 1?

The INTEGER type is a 32 bit signed int, which means numbers bigger than 2^31 cannot be stored in a column with this type.
Use BIGINT instead:

CREATE TABLE Members(
Member_id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
Identity_no BIGINT NOT NULL UNIQUE,
Member_Name varchar(80) NOT NULL,
Member_Surname varchar(80) NOT NULL,
Member_Phone CHAR(11),
MemberCityID INTEGER NOT NULL,
FOREIGN KEY(MemberCityID) REFERENCES Cities(City_id));


Related Topics



Leave a reply



Submit