Python Forum
MUSIC QUIZ - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: MUSIC QUIZ (/thread-28070.html)



MUSIC QUIZ - charlieroberrts - Jul-03-2020

Hello

I am currently coding a music quiz game. Here is my current code.

pop00_tries_left = 2
score = 0

while pop00_tries_left > 0:
    with open("2000POPSONGLIST.txt", "r") as pop00songs_file:
        with open("2000POPARTISTLIST.txt", "r") as pop00artists_file:
            pop00songs_and_artists = [(pop00song.rstrip('\n'), pop00artist.rstrip('\n'))
                                      for (pop00song, pop00artist) in zip(pop00songs_file, pop00artists_file)]
            random00pop_song, random00pop_artist = random.choice(pop00songs_and_artists)
            pop00songs_intials = "".join(item[0].upper() for item in random00pop_song.split())
            print("The songs' initials are", pop00songs_intials, "and the name of the artist is", random00pop_artist)
            pop00guess = input("Guess the name of the song! ")
            pop00_tries_left = pop00_tries_left - 1
    if pop00guess == random00pop_song:
        print("Well Done!")
        score = score + 1
        time.sleep(0.5)
        print("Your Score Is", score)
        pop00_tries_left = 2
    else:
        pop00guess = input("Nope! Try Again! Guess the name of the song again! ")
        pop00_tries_left = pop00_tries_left - 1
If the person guesses correctly on their second guess (which is the else part) how do i get the loop to go back to the start?
With how the code is currently, if the user guessed the song the first try it works and puts another song guess on. However, if the user gets the first guess wrong, the code goes to the 'else' bit at the bottom of the code i pasted in, if they get the code right on this last attempt instead of giving them a nice guess like the code should do, it just ends the code.

Any help would be great.


RE: MUSIC QUIZ - DT2000 - Jul-03-2020

Please use proper tags when posting code, errors so that members can follow your code.


RE: MUSIC QUIZ - menator01 - Jul-03-2020

I would recommend putting your files in a dict. Maybe something like this.

#! /usr/bin/env python3

import random as rnd

songlist = {
    'firehouse': 'kiss',
    'mr. speed': 'kiss',
    'forever': 'kiss',
    'god gave rock and roll to you': 'kiss',
    'smoke on the water': 'deep purple',
    'godzilla': 'blue oyster cult',
    'jamie\'s crying': 'van halen',
    'running with the devil': 'van halen',
    'how blue can you get': 'jeff healey',
    'too late now': 'jeff healey',
    'come together': 'jeff healey',
    'lost in your eyes': 'jeff healey'
}

score = 0;
tries = 3

while tries > 0:
    try:
        random_song = rnd.choice(list(songlist.items()))
        intials = []
        for first_letter in random_song[0].split():
            intials.append(first_letter[0].capitalize())

        print(f'The intials for the song are {" ".join(intials)} and the artist is {random_song[1].title()}')
        print('Can you guess the song?')
        song = input('>> ')
        if song.lower().strip() == random_song[0].lower().strip():
            print(f'Well done! You have {tries - 1} tries left. Your score is {score + 1}.')
            tries -= 1
            score += 1
            continue
        else:
            print(f'That is incorrect. You have {tries - 1} tries left. Your score is {score}.')
            tries -= 1
            continue

        break
    except ValueError as error:
        print(error)
Output:
The intials for the song are C T and the artist is Jeff Healey Can you guess the song? >> come together Well done! You have 2 tries left. Your score is 1. The intials for the song are R W T D and the artist is Van Halen Can you guess the song? >> run with the dogs That is incorrect. You have 1 tries left. Your score is 1. The intials for the song are M S and the artist is Kiss Can you guess the song? >> mr. speed Well done! You have 0 tries left. Your score is 2.