jeudi 4 octobre 2018

I am receiving: IndexError: list assignment index out of range. Can't seem to fix it

at the moment, i am trying to create a python application that will take my code, create a list with songs in it. Then create a random question from the randomly chosen questions from the song list. Confusing i know.

This is my code and i hope someone can help. I seem to get the error:

'del question[count]
IndexError: list assignment index out of range'.

From my understanding i don't think it is out of range and i have spent hours trying to figure it out. This is the code i have:

import csv, random

#creating random questions
def random_songs(username):
    songBook = open('songs.csv', 'r')
    con_song = csv.reader(songBook)
    user = open('user_data.csv', 'a')
    con_user= csv.writer(user, delimiter = ',')
    class data():
        songs = []
        count = 0
        song_name = []
        artist_name = []
        song_answer = []
        song_question = []


    #Setting the song name and artist into a list.
    for i in con_song:
        for x in i:
            data.songs.append(x)

    #Splitting up the data from the csv file into song name and artist name.
    for length in range(0, len(data.songs)):
        if data.count % 2 == 0:
            data.song_name.append(data.songs[data.count])
            data.count += 1
        else:
            data.artist_name.append(data.songs[data.count])
            data.count += 1

    #Randomly chosing a song
    for i in range(0, 1):
        number = random.randint(0, len(data.song_name))
        data.song_answer.append(data.song_name[number])
    #Generating the question
    #print(data.song_answer)

    question = []
    for s in data.song_answer:
        question = list(s)
        print(s)
        length_s = int(len(s) -1)
        count = 1
        for i in range(length_s):
            if count <= length_s and count > 1:
                print('good')
                count = int(count)
                del question[count]
                count += 1
            else:
                print('bad')
                count += 1



random_songs('James')

Here is the song csv file project on the link: https://drive.google.com/open?id=1LxPlKF0pduXE1_r7WWzlwzMZ0mKeY4yY

I hope, that you can help or this will help you once solved.

Thanks in advance. James McCannon




Aucun commentaire:

Enregistrer un commentaire