Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

import random Define the artists and monthly Spotify listeners by genre artistsbygenre =…

  1. 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? (1) Hip-Hop or (2) Pop ") if str(genre) not in artists_by_genre and str(genre) not in ['1', '2']: print("Invalid genre. Please choose Hip-Hop, Pop, 1, or 2.") continue

    artists_listeners = artists_by_genre[str(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()
    
        if guess_lower == 'quit':
            print("Thanks for playing!")
            break
    
        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
    
        else:
            print(
                f"\nSorry, you guessed incorrectly. {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.\n")
            print(f"Your final score is {score}.")
            play_again = input("Would you like to play again? (y/n) ")
            if play_again.lower() == 'n':
                print("Thanks for playing!")
                break
            else:
                score = 0
                first_artist, second_artist = random.sample(list(artists_listeners.keys()),
                                                            2)  # select new artists for the next round
    

    why do i get this error :

    C:\Users\elias\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\elias\PycharmProjects\pythonProject\main.py Which music genre do you want to play with? (1) Hip-Hop or (2) Pop 2 Traceback (most recent call last): File "C:\Users\elias\PycharmProjects\pythonProject\main.py", line 28, in artists_listeners = artists_by_genre[str(genre)] ~~~~~~~~~~~~~~~~^^^^^^^^^^^^ KeyError: '2'

    Process finished with exit code 1

  2. Assistant

    The error is occurring because the key '2' does not exist in the dictionary artists_by_genre. You need to change the condition if str(genre) not in artists_by_genre and str(genre) not in ['1', '2']: to if str(genre) not in ['1', '2']: to only allow valid inputs. Then, you can convert the user input to the corresponding key in the dictionary using genre = 'Hip-Hop' if genre == '1' else 'Pop'. Here's the updated code:

    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? (1) Hip-Hop or (2) Pop ")
        if str(genre) not in ['1', '2']:
            print("Invalid input. Please enter 1 or 2.")
            continue
    
        genre = 'Hip-Hop' if genre == '1' else 'Pop'
        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()
    
            if guess_lower == 'quit':
                print("Thanks for playing!")
                break
    
            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
    
            else:
                print(
                    f"\nSorry, you guessed incorrectly. {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.\n")
                print(f"Your final score is {score}.")
                play_again = input("Would you like to play again? (y/n) ")
                if play_again.lower() == 'n':
                    print("Thanks for playing!")
                    break
                else:
                    score = 0
                    first_artist, second_artist = random.sample(list(artists_listeners.keys()),
                                                                2)  # select new artists for the next round```
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.