import random Define the artists and monthly Spotify listeners by genre artistsbygenre =…
- User
import random
Define the artists and monthly Spotify listeners by genre
artists_by_genre = { ' (1) Hip-Hop': { '21 Savage': 60358167, 'A Boogie Wit Da Hoodie': 18379137, '$NOT': 7781046, '9lokknine': 1680245, 'Ayo & Teo': 1818645 }, ' (2) Pop': { 'Ariana Grande': 81583006, 'Dua Lipa': 35650224, 'Ed Sheeran': 66717265, 'Justin Bieber': 46540612, 'Lady Gaga': 24622751 } }
while True: score = 0 genre = input("Which music genre do you want to play with? (1) Hip-Hop or (2) Pop ") if genre not in ['1', '2']: print("Invalid genre. Please choose Hip-Hop, Pop,") continue
artists_listeners = artists_by_genre[f" ({genre}) {'Hip-Hop' if genre == '1' else 'Pop'}"] artist_names = list(artists_listeners.keys()) while True: first_artist, second_artist = random.sample(artist_names, 2) guess = input(f"\nWhich artist has more monthly Spotify listeners - 1. {first_artist} or 2. {second_artist}? ") guess_lower = guess.strip().lower() if guess_lower == 'quit': print("Thanks for playing!") break elif guess_lower not in ['1', '2', first_artist.lower(), second_artist.lower()]: print("Invalid input. Please enter the name of one of the two artists, '1' or '2', or 'quit' to end the game.") continue elif guess_lower == first_artist.lower() or guess_lower == '1' and artists_listeners[first_artist] > \ artists_listeners[second_artist]: print( f"You guessed correctly! {first_artist} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.") score += 1 first_artist, second_artist = random.sample(list(artists_listeners.keys()), 2) # select new artists for the next round continue elif guess_lower == second_artist.lower() or guess_lower == '2' and artists_listeners[second_artist] > \ artists_listeners[first_artist]: print( f"You guessed correctly! {second_artist} had {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners and {first_artist} has {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners.") score += 1 first_artist, second_artist = random.sample(list(artists_listeners.keys()), 2) # select new artists for the next round continue else: print( f"\nSorry, you guessed incorrectly. {first_artist} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.\n") print(f"Your final score is {score}.") play_again = input("Would you like to play again? (y/n) ") if play_again.lower() == 'n': print("Thanks for playing!") break else: score = 0 first_artist, second_artist = random.sample(list(artists_listeners.keys()), 2) # select new artists for the next round continue - Assistant
The game asks which genre you want to play even after you say you don't want to play again because of the
continuestatement used in theif play_again.lower() == 'n'block. When the player chooses not to play again, thecontinuestatement takes the control flow back to the outerwhile Trueloop, where the game asks the player to choose a music genre again. To fix this, the outerwhile Trueloop can be wrapped in aplay_game()function that can be called again if the player chooses to play again. This way, the control flow will always start from the beginning of the function, and the player will be prompted for the music genre before starting a new game. - User
how could i pu this change in the code?
- Assistant