R
R
rosh1k2022-02-20 14:57:02
Python
rosh1k, 2022-02-20 14:57:02

IndexError: list index out of range why so?

There is such a file, which contains such data as: game, login, password. 621227dce52b5868351452.png
Next, we need the computer to output like this:
Game: .. Login: .. Password: ..
The code is like this:

import os
import csv
from tkinter import *
import tkinter as tk
from tkinter import filedialog
import webbrowser as web 



def close_window():
  global login
  login = login_entry.get()
  root.destroy()

def choose_file():
  global file_path 
  file_path = filedialog.askopenfilename(filetypes = [("CSV", ".csv")])


root = Tk()
root.title('Пароли')
root.geometry('400x260')

login_label = Label(root, text = 'Введите логин')
login_label.pack(anchor = CENTER)
login_entry = Entry(root)
login_entry.pack(anchor = CENTER)
Button_choose_file = Button(root, text = "Выбрать файл" , command =choose_file)
Button_choose_file.pack(anchor = S)
Button = Button(root, text = "Начать" , command =close_window)
Button.pack()
root.mainloop()


with open (file_path , 'r')  as f:
  a = csv.reader(f, delimiter = ';')
  s = 0
  data_csv_read = ""
  for i in a:
    s+=1

    game_csv , login_csv , password_csv = i
    data_csv_read.append((game_csv, login_csv, password_csv))

games_len = len(data_csv_read)

print(data_csv_read)
print(games_len)
games_len = int(games_len)
for i in range(games_len):
  game_plus = int(0)
  login_plus = int(1)
  password_plus = int(2)
  print('Игра: ' + str(data_csv_read[game_plus]) + " Логин: " + str(data_csv_read[login_plus]) + " Пароль: " + str(data_csv_read[password_plus]))
  game_plus += 1
  login_plus += 1
  password_plus += 1

But when trying to do this:
Traceback (most recent call last):
  File "D:\!all\rm\passwordik.py", line 55, in <module>
    print('Игра: ' + str(data_csv_read[game_plus]) + " Логин: " + str(data_csv_read[login_plus]) + " Пароль: " + str(data_csv_read[password_plus]))
IndexError: list index out of range

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2022-02-20
@rosh1k

Well, you clearly wrote list index out of range. Have you tried to translate this expression? What about understanding?
You have two entries in the file.
Here data_csv_read[password_plus] you probably think that you are accessing the "password" field of your current record? In fact, you are accessing the second element of the data_csv_read list, because you wrote password_plus = int(2) beforehand. Those. in fact we have and you have only two such records. obviously it was necessary to write something like But this, of course, is not all the problems that you have. To put it mildly, this fragment causes bewilderment:
data_csv_read[2]
data_csv_read[i][password_plus]

for i in range(games_len):
  game_plus = int(0)
  login_plus = int(1)
  password_plus = int(2)
 ..........
  game_plus += 1
  login_plus += 1
  password_plus += 1

Doesn't it bother you that EVERY iteration of your loop you perform actions with the same values ​​of game_plus, login_plus, password_plus and that the last three lines of the specified fragment are simply meaningless in this case?
But that's not all.
This sequence does not bother you:
data_csv_read = ""
data_csv_read.append((game_csv, login_csv, password_csv))

despite the fact that the string object does not have an append method?
Well, a couple more blunders, which are too early to talk about. Maybe you will read some book on Python first, try to complete the exercises in the order "from simple to complex", and not try to write code that is too tough for you yet? Otherwise, you will keep running after every question and error on the forum until everyone gets tired of answering children's questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question