Список не добавляется правильно (индекс вне диапазона)Python

Программы на Python
Ответить
Anonymous
 Список не добавляется правильно (индекс вне диапазона)

Сообщение Anonymous »

Почему это не работает? < /p>

Код: Выделить всё

current_stock_dict = db.execute("SELECT * FROM current_stocks WHERE c_user_id=:user_id ", user_id=session["user_id"])

# make a list for the mainpage
mainpage_list = [[],[]]

# save the lengh of the dict
lengh_dict = len(current_stock_dict)
price_sum = 0
share_sum = 0

# iterate over all rows in the dict
for i in range(0, (lengh_dict - 1)):

# lookup the symbol in the current stocks
c_symbol = current_stock_dict[i]["c_symbol"]
lookup_symbol = lookup(c_symbol)

# append the symbol to the list for the mainpage
mainpage_list[i].append(c_symbol)

# append the name of the share
share_name = lookup_symbol["name"]
mainpage_list[i].append(share_name)

# append the count of shares for mainpage
c_count = current_stock_dict[i]["c_count"]
mainpage_list[i].append(c_count)

# append the current price
share_price = lookup_symbol["price"]
mainpage_list[i].append("$" + str(share_price))

# append the total price of all shares
total_price = float(share_price) * int(c_count)
mainpage_list[i].append("$" + str(total_price))

# count up the price and shares
price_sum += total_price
share_sum += c_count
< /code>
Когда я запускаю свой веб -сайт через колбу, я получаю: < /p>
**IndexError: list index out of range**
in the line:
``` mainpage_list[i].append(c_symbol) ```
(and i guess if it did not allready fail there i'd get it for the rest of the lines too).
до тех пор, пока lengh_dict = len (current_stock_dict) составляет 3 или менее (поэтому таблица базы данных имеет 3 строки или меньше), код работает.

Подробнее здесь: https://stackoverflow.com/questions/621 ... t-of-range
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»