while True:
score = 0 # reset score at the beginning of each game
first_artist, second_artist = random.sample(list(artists.keys()), 2)
code
# Load the images for the artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
while True:
# Display the images
first_artist_image.show()
second_artist_image.show()
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 or 'quit' to end the game.")
elif guess_lower == first_artist.lower() or guess_lower == '1' and artists[first_artist]["listeners"] > artists[second_artist]["listeners"]:
print(f"You guessed correctly! {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
score += 1
first_artist, second_artist = random.sample(list(artists.keys()), 2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
continue
elif guess_lower == second_artist.lower() or guess_lower == '2' and artists[second_artist]["listeners"] > artists[first_artist]["listeners"]:
print(f"You guessed correctly! {second_artist.title()} had {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
score += 1
first_artist, second_artist = random.sample(list(artists.keys()), 2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
continue
else:
print(f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 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!")
quit()
else:
score = 0
first_artist, second_artist = random.sample(list(artists.keys()), 2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
break
is there a other way i can make the pictures appear? now they open in a different window and i need to close it each time
Assistant
Yes, you can display the images directly within the console using the IPython.display module. Here is an example:
code
from IPython.display import display
...
# Display the images
display(first_artist_image)
display(second_artist_image)
This will show the images within the console output instead of opening them in a separate window.
User
do i first need to install ipython
Assistant
If you are using a Jupyter Notebook or JupyterLab, IPython.display is already installed and you can use it without any extra installation. If you are using a different environment, you may need to install it first.
You can install IPython using pip. Open a terminal and enter:
code
pip install ipython
Once it is installed, you can import the display function from the IPython.display module to show the images within the console output.
User
from IPython.display import display
import random
from PIL import Image
Define the artists and their monthly Spotify listeners
while True:
score = 0 # reset score at the beginning of each game
first_artist, second_artist = random.sample(list(artists.keys()), 2)
code
Assistant
Yes, it looks good! The IPython.display module should display the images directly within the notebook output.
User
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1200x675 at 0x217CF9C22D0>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=700x466 at 0x217CFA7D590>
Which artist has more monthly Spotify listeners - 1. 21 Savage or 2. 9Lokknine? 1
You guessed correctly! 21 Savage had 60.4M monthly Spotify listeners and 9Lokknine has 1.7M monthly Spotify listeners.
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=4479x4479 at 0x217CFA7DC90>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1200x675 at 0x217CFA7DBD0>
Which artist has more monthly Spotify listeners - 1. $Not or 2. 21 Savage?
now the pictures are showed like this, am i suppose to open the code with a different app? now i use pycharm
Assistant
It seems like the images are still displayed using the traditional way, without IPython.display. This might be related to using PyCharm, which does not offer the same level of output formatting as Jupyter notebooks or JupyterLab.
You can try running the code in a Jupyter Notebook or JupyterLab to see the images displayed directly in the output. If that is not an option for you, you can still view the images opening a new window for each one using the Image.show() function provided by the PIL library.
User
how do i install pillow
Assistant
You can install Pillow using pip. Open a terminal (or command prompt on Windows) and enter:
code
pip install Pillow
This will install the Python Imaging Library (PIL) fork called Pillow. Once it is installed, you can use the Image module of the Pillow package to open, create, and manipulate images in your Python code.
User
how do i install pip
Assistant
pip is usually available by default when you install Python on your computer. However, if it is not installed or you need to upgrade, you can install it using the following steps:
Download the get-pip.py script from the official Python package site. You can download the script by opening this link in your web browser: https://bootstrap.pypa.io/get-pip.py
Open a terminal (or command prompt on Windows) and navigate to the directory where you saved the get-pip.py script using the cd command.
Run the following command:
code
python get-pip.py
The above command will start the installation process and pip will be installed on your computer.
You can also use a package manager, such as apt-get or yum, to install pip on Linux or other Unix-like systems. For example, on Ubuntu, you can run the following command:
code
sudo apt-get install python-pip
On macOS, you can install pip through Homebrew by running:
# Load the images for the artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
while True:
# Display the images
display(first_artist_image)
display(second_artist_image)
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 or 'quit' to end the game.")
elif guess_lower == first_artist.lower() or guess_lower == '1' and artists[first_artist]["listeners"] > \
artists[second_artist]["listeners"]:
print(
f"You guessed correctly! {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
score += 1
first_artist, second_artist = random.sample(list(artists.keys()),
2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
continue
elif guess_lower == second_artist.lower() or guess_lower == '2' and artists[second_artist]["listeners"] > \
artists[first_artist]["listeners"]:
print(
f"You guessed correctly! {second_artist.title()} had {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
score += 1
first_artist, second_artist = random.sample(list(artists.keys()),
2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
continue
else:
print(
f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 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!")
quit()
else:
score = 0
first_artist, second_artist = random.sample(list(artists.keys()),
2) # select new artists for the next round
# Load the images for the new artists
first_artist_image = Image.open(artists[first_artist]["image"])
second_artist_image = Image.open(artists[second_artist]["image"])
break