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

what should i change : import random from PIL import Image artistslisteners = { 'Lil…

  1. User

    what should i change : import random from PIL import Image

    artists_listeners = { 'Lil Baby': 30600740, 'Lil Durk': 20244848, 'Lil Keed': 2288807, # add more artists here }

    images = { 'Lil Baby': 'C:/Users/elias\Pictures/Album covers/lil_baby.jpg', 'Lil Durk': 'C:/Users/elias\Pictures/Album covers/lil_durk.jpg', 'Lil Keed': 'C:/Users/elias\Picture/\Album covers/lil_keed.jpg', # add more images here }

    score = 0

    while True: first_artist = random.choice(keys) second_artist = random.choice(keys)

    while first_artist == second_artist: second_artist = random.choice(keys)

    Load the images of the artists

    first_image = Image.open(images[first_artist])
    second_image = Image.open(images[second_artist])
    

    Display the images

    first_image.show()
    second_image.show()
    

    while True: guess = input(f"\nWhich artist has more monthly Spotify listeners - 1. {first_artist.title()} or 2. {second_artist.title()}? ") guess_lower = guess.strip().lower()

    if 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, type 'quit' to end the game or enter 1 or 2 to make a guess.")
    
    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.title()} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.")
      score += 1
      break
    
    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.title()} had {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners.")
      score += 1
      break
    
    else:
      print(f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists_listeners[first_artist] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists_listeners[second_artist] / 1e6:.1f}M monthly Spotify listeners.\n")
      score = 0
      play_again = input("Would you like to play again? (y/n/Ent) ")
      if play_again.lower() == 'n':
        print("Thanks for playing!")
        quit()
    

    Close the images of the artists

    first_image.close()
    second_image.close()
    
    print(f"\nYour score is {score}.") 
    

    ======== RESTART: C:\Users\elias\Documents\Code\Python - Freegpt\test.py ======= Traceback (most recent call last): File "C:\Users\elias\Documents\Code\Python - Freegpt\test.py", line 23, in first_artist = random.choice(keys) NameError: name 'keys' is not defined

  2. Assistant

    Change "keys" to "artists_listeners.keys()" in line 23 to fix the NameError.

  3. User

    like this?while True: first_artist = random.choice(artists_listeners.keys()) second_artist = random.choice(artists_listeners.keys())

  4. Assistant

    Yes, that is correct!

  5. User

    Traceback (most recent call last): File "C:\Users\elias\Documents\Code\Python - Freegpt\test.py", line 23, in first_artist = random.choice(artists_listeners.keys()) File "C:\Users\elias\AppData\Local\Programs\Python\Python311\Lib\random.py", line 374, in choice return seq[self._randbelow(len(seq))] TypeError: 'dict_keys' object is not subscriptable

  6. Assistant

    This error occurs because in Python 3.x, dict.keys() returns a dictionary view object that is not subscriptable. To resolve this error, you can cast the dictionary view object to a list or use the dict.items() method instead of dict.keys().

    Try changing the line 23 to:

    first_artist = random.choice(list(artists_listeners.keys()))
    second_artist = random.choice(list(artists_listeners.keys()))
    

    Or

    first_artist, _ = random.choice(list(artists_listeners.items()))
    second_artist, _ = random.choice(list(artists_listeners.items()))
    

    This should resolve the TypeError.

  7. User

    Traceback (most recent call last): File "C:\Users\elias\Documents\Code\Python - Freegpt\test.py", line 27, in second_artist = random.choice(keys) NameError: name 'keys' is not defined

  8. Assistant

    It looks like you have missed removing the unused line of code in the previous iteration. Please make sure that you have removed or commented out the line keys = artists_listeners.keys() in the code.

    You can fix the NameError by changing the second line inside your while True loop to:

    second_artist = random.choice(artists_listeners.keys())
    

    or

    second_artist, _ = random.choice(list(artists_listeners.items()))
    

    This should resolve the NameError.

  9. User

    Traceback (most recent call last): File "C:\Users\elias\Documents\Code\Python - Freegpt\test.py", line 24, in second_artist = random.choice(artists_listeners.keys()) File "C:\Users\elias\AppData\Local\Programs\Python\Python311\Lib\random.py", line 374, in choice return seq[self._randbelow(len(seq))] TypeError: 'dict_keys' object is not subscriptable

  10. Assistant

    The TypeError message suggests that the dictionary view object returned by the dict.keys() method cannot be used with the choice() method. In Python 3.x, dict.keys() returns a dictionary view object that is not subscriptable.

    You can fix this by converting the dictionary view object to a list or using the dict.items() method to get a list of key-value pairs. Here are two options to fix the TypeError:

    while True:
        first_artist = random.choice(list(artists_listeners.keys()))
        second_artist = random.choice(list(artists_listeners.keys()))
    

    or

    while True:
        first_artist, _ = random.choice(list(artists_listeners.items()))
        second_artist, _ = random.choice(list(artists_listeners.items()))
    

    These should fix the TypeError that you are seeing.