Search code examples
pythonfileloopsleaderboard

how to make code ask another question and store last one


I am doing a project in school which is asking me to make a music quiz in python that reads a file, displays the first letters of each word of the song and the artist (e.g Dave F F). In my file I have a list of 10 song names where python fetches a random line and does the displaying. It must be from a file (mine is notepad) The user must have 2 chances to guess the name of the song, and if they don't then the game ends. The problem that I have is I cannot get my code to ask another question, and store the last one asked so that it doesn't ask it again (e.g if the first question is Dave and F F, I want it to not come again). I would also appreciate it if I was shown how to get python to display a leaderboard. Could answers please be the full code with improvements as i'm not good with indents and putting code In the right place.

I have already gave the user 2 chances to get the song right, and If they dont then the program ends, but doesn't loop to the start.

import random

with open("songlist.txt", "r") as songs_file:
    with open("artistlist.txt", "r") as artists_file:
        songs_and_artists = [(song.rstrip('\n'), artist.rstrip('\n'))
                             for (song, artist) in zip(songs_file,     artists_file)]

random_song, random_artist = random.choice(songs_and_artists)
songs_intials = "".join(item[0].upper() for item in random_song.split())


print("The songs' initials are", songs_intials, "and the name of the artist is", random_artist)


nb_tries_left = 3
guess = input("Guess the name of the song! ")
nb_tries_left -= 1

finished = False
while not finished:
    answer_found = (guess == random_song)
    if not answer_found:
        guess = input("Nope! Try again! ")
        nb_tries_left -= 1
    elif answer_found:
        print("The songs' initials are", songs_intials, "and the name of the artist is", random_artist)

    finished = (answer_found or nb_tries_left <= 0) 

if answer_found:

The songs' initials are LT and the name of the artist is Fredo Guess the name of the song! Like That The songs' initials are LT and the name of the artist is Fredo Well done!

Python then doesn't ask another question, and I don't know If it will be that one again.

getting it wrong on purpose outputs this:

The songs' initials are CS and the name of the artist is 50 Cent
Guess the name of the song! candysong
Nope! Try again! carpetshop
Nope! Try again! coolsong
Sorry, you've had two chances. Come back soon!
>>> 

Solution

  • First You want to get 2 unique songs. To do that you could use random.sample. For your use case, It is

    indexes = random.sample(range(len(songs_and_artists)), 2) # 2 random songs (sampling without replacement)
    # song 1
    random_song, random_artist = songs_and_artists[indexes[0]]
    # song 2
    random_song, random_artist = songs_and_artists[indexes[1]]
    

    Additionally, I recommend you to put your code to the function and use it with each selected song.