how can i change my codeso the player has to choose between different genres of music…
- User
how can i change my codeso the player has to choose between different genres of music but after that the game is the same concept :
import random
artists_listeners = { 'Young Thug': 28887553, 'Yungeen Ace': 1294188, }
keys = list(artists_listeners.keys())
score = 0
while True: first_artist = random.choice(keys) second_artist = random.choice(keys)
while first_artist == second_artist: second_artist = random.choice(keys)
while True: guess = input(f"\nWhich artist has more monthly Spotify listeners - 1. {first_artist.title()} or 2. {second_artist.title()}? ") guess_lower = guess.strip().lower()
codeif guess_lower == 'quit': print("Thanks for playing!") quit() 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 or 'quit' to end the game, or enter 1 or 2 to make a guess.") 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.title()} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.") score += 1 break 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.title()} had {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners.") score += 1 break else: print(f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.\n") score = 0 play_again = input("Would you like to play again? (y/n) ") if play_again.lower() == 'n': print("Thanks for playing!") quit() - Assistant
- User
what changed and what can i copy from the previous code
- Assistant
Here are the changes I made to the code to allow the player to choose between different genres of music:
- I created a dictionary called
genre_artistswhich contains the different genres of music as keys and lists of artists belonging to those genres as values. - I added a while loop that prompts the player to choose a genre by typing its name. The loop keeps running until the player chooses a valid genre.
- Instead of choosing two random artists from
keys, I setchosen_artiststo be the list of artists belonging to the chosen genre, and then usedrandom.sample()to choose two random artists from that list.
Here's what you can copy from the previous code:
- The dictionary
artists_listenersand the initialization ofkeysbased on this dictionary. You may need to modify this dictionary to include more artists and adjust their listener counts based on current Spotify data. - The basic game mechanics, such as the outer while loop that keeps the game running until the player quits, and the inner while loop that prompts the player to guess which artist has more monthly Spotify listeners.
- I created a dictionary called