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 = { 'Hip-Hop': { '21 Savage': 60358167, 'A Boogie Wit Da Hoodie': 18379137, '$NOT': 7781046, '9lokknine': 1680245, 'Ayo & Teo': 1818645 }, 'Pop': { 'Ariana Grande': 81583006, 'Dua Lipa': 35650224, 'Ed Sheeran': 66717265, 'Justin Bieber': 46540612, 'Lady Gaga': 24622751 } } while True: score = 0 # reset score at the beginning of each game genre = input("Which music genre do you want to play with? (Hip-Hop/Pop) ") if genre not in artists_by_genre: print("Invalid genre. Please choose Hip-Hop or Pop.") continue
artists_listeners = artists_by_genre[genre] 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()
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.") 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 - Assistant
There is an indentation error in the code. The
continuestatement after theprintstatement in theifblock for invalid genre is not indented correctly, causing the while loop to continue even if a valid genre is chosen.Here's the corrected code:
codewhile True: score = 0 # reset score at the beginning of each game genre = input("Which music genre do you want to play with? (Hip-Hop/Pop) ") if genre not in artists_by_genre: print("Invalid genre. Please choose Hip-Hop or Pop.") continue artists_listeners = artists_by_genre[genre] artist_names = list(artists_listeners.keys()) # rest of the code...Note that the indentation of the
artists_listenersandartist_namesassignments should be at the same level as theifstatement, so that they only occur when a valid genre is chosen. - User
Which music genre do you want to play with? (Hip-Hop/Pop) Pop Which music genre do you want to play with? (Hip-Hop/Pop)
it stays like thiss - Assistant
The program seems to be stuck waiting for user input. This could be due to a couple of reasons:
-
The program is waiting for the user to input a genre, but the input is not being registered for some reason. This could happen if there is a problem with the terminal or if there is a bug in the code that is causing the program to hang.
-
The program is waiting for user input, but the prompt is hidden or not visible on the screen. This could happen if the terminal window is too small to display the prompt or if there is some other issue with the terminal configuration.
To troubleshoot the issue, try the following:
-
Check if there are any error messages or stack traces being printed to the console. If there are, this could indicate a bug in the code that needs to be fixed.
-
Try running the program in a different terminal or command prompt window to see if the issue persists.
-
Verify that the terminal window is large enough to display the full prompt and that there are no other issues with the terminal configuration.
If none of these steps fix the issue, it may be helpful to post the full code and any error messages that are being printed to help diagnose the problem.
-