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
def new_game(): 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) return first_artist, second_artist
def guess_input(first_artist, second_artist): guess = input(f"\nWhich artist has more monthly Spotify listeners - {first_artist.title()} or {second_artist.title()}? ") guess_lower = guess.lower() if guess_lower == 'quit': return None, False elif guess_lower != first_artist.lower() and guess_lower != second_artist.lower(): print("Invalid input. Please enter the name of one of the two artists or 'quit' to end the game.") return guess_input(first_artist, second_artist) else: return guess_lower, True
while not game_over:
start a new game
first_artist, second_artist = new_game()
ask the user to guess which artist has more monthly Spotify listeners
while True: guess, should_continue = guess_input(first_artist, second_artist) if should_continue: break
check the user's guess and update the score
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 else: print(f"You guessed incorrectly. {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.")
continue_input = input("Enter 'y' to continue playing or any other key to quit. ") if continue_input.lower() != 'y':
display the current score and ask the user if they want to continue playing
print(f"\nYour score is {score}.") print("Thanks for playing!") game_over = True
would this work?
- Assistant
As an AI language model, I cannot run code directly. However, the code itself looks correct and would likely work.
- User
i get this error File "main.py", line 58 first_artist, second_artist = new_game() ^ IndentationError: expected an indented block
** Process exited - Return Code: 1 **
- Assistant
The
IndentationErrorin Python usually means there's an issue with the indentation of the code. Please make sure that all the statements within the while loop are indented the same amount. You might need to check if you have mixed tabs and spaces in your code as well. It's generally recommended to use only spaces for indentation in Python. - User
what should i do?
- Assistant
You need to check your code's indentation. Perhaps, the
new_game()function and the following while loop should be indented like this:while not game_over: # start a new game first_artist, second_artist = new_game() # ask the user to guess which artist has more monthly Spotify listeners while True: guess, should_continue = guess_input(first_artist, second_artist) if should_continue: break # check the user's guess and update the score 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 else: print(f"You guessed incorrectly. {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.") continue_input = input("Enter 'y' to continue playing or any other key to quit. ") if continue_input.lower() != 'y': # display the current score and ask the user if they want to continue playing print(f"\nYour score is {score}.") print("Thanks for playing!") game_over = True