Я пробовал разные подходы в течение 3 часов и просто не понимаю, почему это не работает.
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
Когда я запускаю свой веб-сайт через Flask, я получаю сообщение об ошибке: IndexError: индекс списка вне диапазона в строке: mainpage_list[i].append(c_symbol)
(и я думаю, если бы он не завершился ошибкой, я бы получил его и для остальных строк).
Пока lengh_dict = len(current_stock_dict)
равно 3 или меньше (так что SQL db имеет 3 строки или меньше) сообщение об ошибке не появляется, и код работает нормально. Я не совсем понимаю списки (и многомерные списки) в python, поэтому я был бы счастлив, если бы кто-нибудь мог объяснить мне мою ошибку.
Обычно я распечатываю много вещей и просто пробую где ошибка в том, что я только начал использовать flask, и я не могу распечатать списки, dicts или что-то еще, если код останавливается до достижения ошибки.
Всем уже спасибо за вашу помощь !!!