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)
2
  • Does this answer your question? How can I display an image from a file in Jupyter Notebook? Commented Dec 15, 2021 at 9: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: Commented Dec 15, 2021 at 9:52

1 Answer 1

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))
   
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks cel . Also, it worked for me, without specifying URL, just replacing data by filename ='image.jpg' where, image is located in working directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.