import random artistslisteners = { ‘Lil Baby’: 58738173, ‘Roddy Ricch’: 34447081,…
- User
import random
artists_listeners = { ‘Lil Baby’: 58738173, ‘Roddy Ricch’: 34447081, ‘DaBaby’: 29659518, ‘Polo G’: 27204041, ‘NLE Choppa’: 11423582, ‘Lil Tjay’: 16873693, ‘Lil Durk’: 19827615, ‘Megan Thee Stallion’: 26685725, ‘Pop Smoke’: 32169881, ‘Lizzo’: 9236657, ‘Migos’: 23242076, ‘Doja Cat’: 33556496, ‘Tyler, The Creator’: 11459242, ‘Saweetie’: 10217413, ‘Cordae’: 4026278, ‘Juice WRLD’: 42092985, ‘Travis Scott’: 52627586, ‘Post Malone’: 65466387, ‘Drake’: 71588843, ‘Kendrick Lamar’: 33841249, ‘J. Cole’: 38726509, ‘Kanye West’: 29204328, ‘Lil Nas X’: 23824455, ‘21 Savage’: 26764030, ‘Lil Uzi Vert’: 40941950, }
keys = list(artists_listeners.keys()) score = 0 game_over = False first_artist = None second_artist = None
def new_game(): global first_artist,second_artist first_artist = random.choice(keys) second_artist = random.choice(keys) while first_artist == second_artist or first_artist == ‘all’ or second_artist == ‘all’: second_artist = random.choice(keys)
check if guess matches both artists in the comparison
if guess_lower in [first_artist.lower(), second_artist.lower()]: matching_artists = [a for a in [first_artist, second_artist] if guess_lower == a.lower()] print(f"Your guess ‘{guess}’ matches both {matching_artists[0].title()} and {matching_artists[1].title()}!“) guess = input(f"Which artist do you want to select - {first_artist.title()} or {second_artist.title()}? “)
check if guess matches only one artist in keys
elif guess_lower in [k.lower() for k in keys]: matching_artists = [key for key in keys if guess_lower == key.lower()] if matching_artists[0] in [first_artist.lower(), second_artist.lower()]: print(f"You guessed {matching_artists[0].title()} and that is correct!”) return guess.lower() else: print(f"You guessed {matching_artists[0].title()} and that is incorrect!”) play_again = input(“Do you want to continue playing? (y/n)”).lower() if play_again == ‘n’: global game_over, score game_over = True score = 0 return guess.lower() else: global first_artist, second_artist score = 0 new_game() return guess.lower()
otherwise, guess does not match any artist
else: print(f"Sorry, the artist ‘{guess}’ was not found in the game. Please try again.“) guess_input()
return guess.lower()
new_game()
while not game_over:
ask the user to guess which artist has more monthly Spotify listeners
guess = guess_input()
if guess == first_artist.lower(): print(f"You guessed correctly! {first_artist.title()} had {int(artists_listeners[first_artist] / 1e6):,}M monthly Spotify listeners and {second_artist.title()} has {int(artists_listeners[second_artist] / 1e6):,}M monthly Spotify listeners.”) score += 1 first_artist = second_artist second_artist = random.choice(keys) while first_artist == second_artist or first_artist == ‘all’ or second_artist == ‘all’: second_artist = random.choice(keys) elif guess == second_artist.lower(): print(f"You guessed correctly! {second_artist.title()} had {int(artists_listeners[second_artist] / 1e6):,}M monthly Spotify listeners and {first_artist.title()} has {int(artists_listeners[first_artist] / 1e6):,}M monthly Spotify listeners.“) score += 1 first_artist = second_artist second_artist = random.choice(keys) while first_artist == second_artist or first_artist == ‘all’ or second_artist == ‘all’: second_artist = random.choice(keys)
- Assistant
It looks like there is an indentation error on line 38. The "global" statement should be indented to match the block it's in (the new_game() function):
def new_game(): global first_artist,second_artist first_artist = random.choice(keys) second_artist = random.choice(keys) while first_artist == second_artist or first_artist == ‘all’ or second_artist == ‘all’: second_artist = random.choice(keys) # check if guess matches both artists in the comparison if guess_lower in [first_artist.lower(), second_artist.lower()]: matching_artists = [a for a in [first_artist, second_artist] if guess_lower == a.lower()] print(f"Your guess ‘{guess}’ matches both {matching_artists[0].title()} and {matching_artists[1].title()}!") guess = input(f"Which artist do you want to select - {first_artist.title()} or {second_artist.title()}? ") # check if guess matches only one artist in keys elif guess_lower in [k.lower() for k in keys]: matching_artists = [key for key in keys if guess_lower == key.lower()] if matching_artists[0] in [first_artist.lower(), second_artist.lower()]: print(f"You guessed {matching_artists[0].title()} and that is correct!") return guess.lower() else: print(f"You guessed {matching_artists[0].title()} and that is incorrect!") play_again = input("Do you want to continue playing? (y/n)").lower() if play_again == ‘n’: global game_over, score game_over = True score = 0 return guess.lower() else: global first_artist, second_artist score = 0 new_game() return guess.lower() # otherwise, guess does not match any artist else: print(f"Sorry, the artist ‘{guess}’ was not found in the game. Please try again.") guess_input() return guess.lower() - User
- Assistant
It looks like there is still an indentation error on line 15. The "global" statement should be indented to match the block it's in (the new_game() function):
def new_game(): global first_artist,second_artist first_artist = random.choice(keys) second_artist = random.choice(keys) while first_artist == second_artist or first_artist == ‘all’ or second_artist == ‘all’: second_artist = random.choice(keys) # check if guess matches both artists in the comparison if guess_lower in [first_artist.lower(), second_artist.lower()]: matching_artists = [a for a in [first_artist, second_artist] if guess_lower == a.lower()] print(f"Your guess ‘{guess}’ matches both {matching_artists[0].title()} and {matching_artists[1].title()}!") guess = input(f"Which artist do you want to select - {first_artist.title()} or {second_artist.title()}? ") # check if guess matches only one artist in keys elif guess_lower in [k.lower() for k in keys]: matching_artists = [key for key in keys if guess_lower == key.lower()] if matching_artists[0] in [first_artist.lower(), second_artist.lower()]: print(f"You guessed {matching_artists[0].title()} and that is correct!") return guess.lower() else: print(f"You guessed {matching_artists[0].title()} and that is incorrect!") play_again = input("Do you want to continue playing? (y/n)").lower() if play_again == 'n': global game_over, score game_over = True score = 0 return guess.lower() else: global first_artist, second_artist score = 0 new_game() return guess.lower() # otherwise, guess does not match any artist else: print(f"Sorry, the artist ‘{guess}’ was not found in the game. Please try again.") guess_input() return guess.lower()