简体   繁体   中英

I am creating a music quiz but seem to be getting the same error at random points in the middle of the quiz

My Code is meant to:

  1. Stores a list of song names and artists in an external file.

  2. Selects a song from the file, displaying the artist and the first letter of each word of the song title.

  3. Allows the user up to two chances to guess the name of the song, stopping the game if they guess a song incorrectly on the second chance.

  4. If the guess is correct, add the points to the player's score depending on the number of guesses.

  5. The Song and Artists name are removed from the array/external file.

  6. Displays the number of points the player has when the game ends.

Currently My code is getting stuck whilst answering the questions and giving me the error: ChosenSong = (songs[RandomNumber]) IndexError: list index out of range

Anyway to fix this would be great!

Currently my code looks like this:

import random
points = 0
x = 1

SongArtists = open("Songs and Artists.txt","r")
ReadValues = SongArtists.readline()
songs = []
artists = []

while len(songs) < len(ReadValues):
    field = ReadValues.split(",")
    songTitle = field[0]
    artistName = field[1]
    songs.append(songTitle)
    artists.append(artistName)

    ReadValues = SongArtists.readline()




print("")

from random import *

while x == 1:

    RandomNumber = (randint(0,len(songs)))
    RandomNumber = RandomNumber + 1
    ChosenSong = (songs[RandomNumber])
    print("")
    print(artists[songs.index(ChosenSong)])

    print("")

    Field = ChosenSong.split(" ")

    lenOfField = len(Field)

    if lenOfField == 1:

        letter = Field[0]
        print(letter[ : 1])
        songs.pop(RandomNumber)
        artists.pop(RandomNumber)



    elif lenOfField == 2:
        letter = Field[0]      
        secondletter = Field[1]
        print((letter[ : 1])+" "+(secondletter[ : 1]))
        songs.pop(RandomNumber)
        artists.pop(RandomNumber)


    elif lenOfField == 3:
        letter = Field[0]       
        secondletter = Field[1]
        thirdletter = Field[2]
        print((letter[ : 1])+" "+(secondletter[ : 1])+" "+(thirdletter[ : 1]))
        songs.pop(RandomNumber)
        artists.pop(RandomNumber)




    elif lenOfField == 4:
        letter = Field[0]       
        secondletter = Field[1]
        thirdletter = Field[2]
        fourthletter = Field[3]
        print((letter[ : 1])+" "+(secondletter[ : 1])+" "+(thirdletter[ : 1])+" "+(fourthletter[ :1]))
        songs.pop(RandomNumber)
        artists.pop(RandomNumber)



    elif lenOfField == 5:
        letter = Field[0]       
        secondletter = Field[1]
        thirdletter = Field[2]
        fourthletter = Field[3]
        fifthletter = Field[4]
        print((letter[ : 1])+" "+(secondletter[ : 1])+" "+(thirdletter[ : 1])+" "+(fourthletter[ :1])+" "+(fifthletter[ :1]))
        songs.pop(RandomNumber)
        artists.pop(RandomNumber)






    print("")    

    guess = input("What is your guess : ").title()

    if guess == ChosenSong:
        points += 3
        print("Correct, You get 3 points! You're score is", points)

    else:

        print("")

        print("Wrong, If you get this wrong You Lose ")

        secondguess = input("What is your guess : ")

        if secondguess == ChosenSong:
            points += 1
            print("Correct, You get 3 points! You're score is", points)        

        else:
            print("Game Over, You Scored " + str(points) + " points")

            ScoreFile = open("Scores.txt","a")
            ScoreFile.write(str(points))
            ScoreFile.write("\n")

            break

Here:

RandomNumber = (randint(0,len(songs)))
RandomNumber = RandomNumber + 1

If your RandomNumber is equal to len(songs) at the initialization, it already is out of songs bounds.

According to Python doc randint(a, b) returns a random integer N such that a <= N <= b .

So remove the second line, and the first become:

RandomNumber = (randint(0, len(songs) - 1))

The problem is with your random song mechanism. randint() returns a number between the range including the extremes. So you are basically getting a random number between 1 and len(songs)+1 which you don't want since indexes end at len(songs)-1.

So you should just be doing

RandomNumber = random.randint(0, len(songs)-1)
ChosenSong = songs[RandomNumber]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM