import random artistslisteners = { ‘$NOT’: 7781046, ‘Yungeen Ace’: 1294188, } Create a…
- User
import random
artists_listeners = { ‘$NOT’: 7781046, ‘Yungeen Ace’: 1294188, }
Create a dictionary that maps the names of the rappers to their corresponding keys in the artists_listeners dictionary
names_to_keys = {} for key, value in artists_listeners.items(): names_to_keys[key.lower()] = key
keys = list(artists_listeners.keys())
score = 0
while True:
Choose two random rappers
first_artist, second_artist = random.sample(keys, 2)
Prompt the user to guess which rappper has more monthly listeners
print(f"\nWhich artist has more monthly Spotify listeners: 1. {first_artist} ({artists_listeners[first_artist]:,}) or 2. {second_artist} ({artists_listeners[second_artist]:,})?") guess = input("Enter the name or number of the artist you think has more monthly Spotify listeners or ‘quit’ to end the game: “) guess = guess.strip().lower()
Check if the user wants to quit the game
if guess == ‘quit’: print(“Thanks for playing!”) quit()
Try to interpret the user input as a number first
try: guess_int = int(guess) if guess_int not in {1, 2}: print(“Invalid input. Please enter 1 or 2 for the number of the artist you think has more monthly listeners, or their name.”) continue
if guess_int == 1: guess_key = first_artist else: guess_key = second_artist
If user input is not a number, try to interpret it as an artist name
except ValueError: guess_key = names_to_keys.get(guess) if guess_key is None or guess_key not in {first_artist, second_artist}: print(“Invalid input. Please enter 1 or 2 for the number of the artist you think has more monthly listeners, or their name.”) continue
Determine the correct answer and update score and last-used numbers if necessary
if artists_listeners[first_artist] >= artists_listeners[second_artist]: correct_index = “1” if first_artist == guess_key else “2” if first_artist != second_artist: last_used_1 = first_artist if correct_index == “1” else second_artist last_used_2 = second_artist if correct_index == “1” else first_artist else: correct_index = “2” if second_artist == guess_key else “1” if first_artist != second_artist: last_used_1 = second_artist if correct_index == “1” else first_artist last_used_2 = first_artist if correct_index == “1” else second_artist
if guess_key == first_artist or guess_key == second_artist: if guess_key == names_to_keys.get(first_artist.lower()): last_used_1 = first_artist elif guess_key == names_to_keys.get(second_artist.lower()): last_used_2 = second_artist
if guess_key == names_to_keys.get(first_artist.lower()): guess_key = first_artist elif guess_key == names_to_keys.get(second_artist.lower()): guess_key = second_artist
if guess_key == correct_index: print(“Congratulations! You guessed correctly.”) score += 1 else: print(f"Sorry, that’s incorrect. The correct answer was {correct_index}.”) score = 0 play_again = input("Would you like to play again? (y/n) “) if play_again.lower() == “n”: print(“Thanks for playing!”) quit()
- Assistant