0

I have to display 2 different images for this riddle in python code. But images are not getting displayed in python jupyter notebook. It keeps on processing and no error is displayed. Can someone guide me, how to display 2 different images in if else condition while taking input from user.

Code:

print("What invention lets you look right through a wall")
ans = ["window", 'Window','WINDOW']

respond = input()
from IPython.display import Image

if respond  in ans:  
    Image(url= "window.jpg", width=200, height=200)
else:
    Image(url= "cat_meme.jpg", width=200, height=200)
cel
  • 30,017
  • 18
  • 97
  • 117
ratika
  • 11
  • 3
  • Does this answer your question? [How can I display an image from a file in Jupyter Notebook?](https://stackoverflow.com/questions/11854847/how-can-i-display-an-image-from-a-file-in-jupyter-notebook) – Tomerikoo Dec 15 '21 at 09:51
  • Just a side note, it would be easier to do something like `ans = 'window'` and then in the if just check `if respond.lower() == ans:` – Tomerikoo Dec 15 '21 at 09:52

1 Answers1

0

You can use IPython.display.display to show the image:

print("What invention lets you look right through a wall") 
ans = ["window", 'Window','WINDOW']

respond = input() 
from IPython.display import Image, display

window_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Window_of_the_house_with_number_17_on_strada_M%C3%A2ntuleasa%2C_from_Bucharest_%28Romania%29.jpg/117px-Window_of_the_house_with_number_17_on_strada_M%C3%A2ntuleasa%2C_from_Bucharest_%28Romania%29.jpg"
door_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Pohjolan_talo_-_Marit_Henriksson_1.jpg/526px-Pohjolan_talo_-_Marit_Henriksson_1.jpg"

if respond in ans:
    display(Image(data=window_url, width=200, height=200))
else: 
    display(Image(data=door_url, width=200, height=200))
   
cel
  • 30,017
  • 18
  • 97
  • 117
  • 1
    Thanks cel . Also, it worked for me, without specifying URL, just replacing data by filename ='image.jpg' where, image is located in working directory – ratika Dec 16 '21 at 05:42