简体   繁体   中英

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 (eg Dave FF). 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 (eg if the first question is Dave and FF, 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!
>>> 

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.

In order to ask more than one question every game you have to do something like this:

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)]

def getSongAndArtist():
    randomIndex = random.randrange(0, len(songs_and_artists))
    return songs_and_artists.pop(randomIndex)


while(len(songs_and_artists) > 0):
    random_song, random_artist = getSongAndArtist()
    #play game with the song

You save the list of songs in a python list and pop a random one out each round as long as you have more songs to play with.

For the leaderboard you have to ask for a user name before you start the game and save a list of usernames and their score and then pick the top ones. you should also figure out how to score the users

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