0

The user enters a state. I would like my function to find the corresponding image and display it. As you can see I only placed the file name in the dictionary. I would need to tell the program to go the the specific file the image is in somehow.

def picture(pictures_of_flowers,state_name):
    # This part of the code I don't know how to do.
    ...


pictures_of_flowers = {
    'Alabama': 'Alabama.jpg'
    ,'Alaska': 'Alaska.jpg'
    ,'Arizona':"Arizona.jpg"
    ,'Arkansas':"arkansas.jfif"
    ,'California':"california.jpg"
    ,'Colorado':"colarado.jpg"
    ,'Connecticut': "connecticut.jpg"
    ,'Delaware':"delaware.jpg"
    ,'Florida':"lorida.jpg"
    ,'Georgia':"georgia.jpg"
    ,'Hawaii':"hawaii.jpg"
    ,'Idaho':"idaho.jpg"
    ,'Illinois': "illinois.jpg"
    ,'Indiana':"indiana.jpg"
    ,'Iowa':"iowa.jpg"
    ,'Kansas':"kansas.jpg"
    ,'Kentucky':"kentucky.jpg"
    ,'Louisiana':"louisiana.jpg"
    ,'Maine':"maine.jpg"
    ,'Maryland':"maryland.jpg"
    ,'Massachusetts':"massachusetts.jpg"
    ,'Michigan':"michigan.jpg"
    ,'Minnesota':"minnesota.jpg"
    ,'Mississippi':"mississipi.jpg"
    ,'Missouri':"missouri.jpg"
    ,'Montana':"montana.jpg"
    ,'Nebraska':"nebraska.jpg"
    ,'Nevada':"nevada.jpg"
    ,'New Hampshire':"new hampshire.jpg"
    ,'New Jersey':"new jersey.jpg"
    ,'New Mexico':"new mexico.jpg"
    ,'New York':"new york.jpg"
    ,'North Carolina':"north carolina.jpg"
    ,'North Dakota':"north dakota.jpg"
    ,'Ohio':"ohio.jpg"
    ,'Oklahoma':"oklahoma.jpg"
    ,'Oregon':"oregon.jpg"
    ,'Pennsylvania':"pennslvania.jpg"
    ,'Rhode Island':"rhodeisland.jpg"
    ,'South Carolina':"south carolina.jpg"
    ,'South Dakota':"south dakota.jpg"
    ,'Tennessee':"tennessee.jpg"
    ,'Texas':"texas.jpg"
    ,'Utah':"utah.jpg"
    ,'Vermont':"vermont.jpg"
    ,'Virginia':"viginia.jpg"
    ,'Washington':"washington.jpg"
    ,'West Virginia':"west virginia.jpg"
    ,'Wisconsin':"wisconsin.jpg"
    ,'Wyoming':"wyoming.jpg"
}

state_name = input("\nEnter STATE Name: ")
picture(pictures_of_flowers,state_name)
martineau
  • 112,593
  • 23
  • 157
  • 280

1 Answers1

1

Here an example:

from PIL import Image

def picture(pictures_of_flowers, state_name):
    if state_name in pictures_of_flowers:  # State found
            with Image.open(pictures_of_flowers[state_name]) as img:
                    img.show()


pictures_of_flowers = {
'Alabama': 'Alabama.jpg'
,'Alaska': 'Alaska.jpg'
,'Arizona':"Arizona.jpg"
,'Arkansas':"arkansas.jfif"
,'California':"california.jpg"
,'Colorado':"colarado.jpg"
,'Connecticut': "connecticut.jpg"
,'Delaware':"delaware.jpg"
,'Florida':"lorida.jpg"
,'Georgia':"georgia.jpg"
,'Hawaii':"hawaii.jpg"
,'Idaho':"idaho.jpg"
,'Illinois': "illinois.jpg"
,'Indiana':"indiana.jpg"
,'Iowa':"iowa.jpg"
,'Kansas':"kansas.jpg"
,'Kentucky':"kentucky.jpg"
,'Louisiana':"louisiana.jpg"
,'Maine':"maine.jpg"
,'Maryland':"maryland.jpg"
,'Massachusetts':"massachusetts.jpg"
,'Michigan':"michigan.jpg"
,'Minnesota':"minnesota.jpg"
,'Mississippi':"mississipi.jpg"
,'Missouri':"missouri.jpg"
,'Montana':"montana.jpg"
,'Nebraska':"nebraska.jpg"
,'Nevada':"nevada.jpg"
,'New Hampshire':"new hampshire.jpg"
,'New Jersey':"new jersey.jpg"
,'New Mexico':"new mexico.jpg"
,'New York':"new york.jpg"
,'North Carolina':"north carolina.jpg"
,'North Dakota':"north dakota.jpg"
,'Ohio':"ohio.jpg"
,'Oklahoma':"oklahoma.jpg"
,'Oregon':"oregon.jpg"
,'Pennsylvania':"pennslvania.jpg"
,'Rhode Island':"rhodeisland.jpg"
,'South Carolina':"south carolina.jpg"
,'South Dakota':"south dakota.jpg"
,'Tennessee':"tennessee.jpg"
,'Texas':"texas.jpg"
,'Utah':"utah.jpg"
,'Vermont':"vermont.jpg"
,'Virginia':"viginia.jpg"
,'Washington':"washington.jpg"
,'West Virginia':"west virginia.jpg"
,'Wisconsin':"wisconsin.jpg"
,'Wyoming':"wyoming.jpg"
}

state_name = input("\nEnter STATE Name: ")
picture(pictures_of_flowers,state_name)
martineau
  • 112,593
  • 23
  • 157
  • 280
Nabil
  • 618
  • 1
  • 8
  • While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Feb 03 '22 at 01:13
  • trouble is that the value in my dictionary does not contain the whole file path. I am wanting to know how I get the program to get that file from the file path ex. "C:\Users\ablev\OneDrive\Desktop\SDEV 300 6384 Building Secure Python Programs\State Images\missouri.jpg" – user3316598 Feb 03 '22 at 01:51
  • 1
    You can store the full path of the directory in a variable then use the function join in the module os.path to get the full path of the image – Nabil Feb 03 '22 at 02:10
  • got it, thanks so much! – user3316598 Feb 03 '22 at 02:31