Question: I need some assistance putting some validation logic into the below. When a user does not enter a correct state (either in the search state
I need some assistance putting some validation logic into the below. When a user does not enter a correct state (either in the search state or update population) if it is not in the dictionary it should display a message and then loop the user back to re-enter.
import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg
state_data = [ {"state": "Alabama" , "capitol": "Montgomery" , "flower": "Camellia" , "population": 4887680 , "flower picture": "flowers/AL.jpg"}, {"state": "Alaska" , "capitol": "Juneau" , "flower": " Ptarmigan" , "population": 735139 , "flower picture": "flowers/AK.jpg"}, {"state": "Arizona" , "capitol": "Phoenix" , "flower": "Blossom of the Saguaro Cactus", "population": 7158020 , "flower picture": "flowers/AZ.jpg"}, {"state": "Arkansas" , "capitol": "Little Rock", "flower": "Apple blossom" , "population": 3009730 , "flower picture": "flowers/AR.jpg"}, {"state": "California" , "capitol": "Sacramento" , "flower": "Golden Poppy" , "population": 39461600 , "flower picture": "flowers/CA.jpg"}, {"state": "Colorado" , "capitol": "Denver" , "flower": "Rocky Mountain Columbine" , "population": 5691290 , "flower picture": "flowers/CO.jpg"}, {"state": "Connecticut" , "capitol": "Hartford" , "flower": "Mountain Laurel" , "population": 3571520 , "flower picture": "flowers/CT.jpg"}, {"state": "Delaware" , "capitol": "Dover" , "flower": "Peach blossom" , "population": 965479 , "flower picture": "flowers/DE.jpg"}, {"state": "Florida" , "capitol": "Tallahassee", "flower": "Orange blossom" , "population": 21244300 , "flower picture": "flowers/FL.jpg"}, {"state": "Georgia" , "capitol": "Atlanta" , "flower": "Cherokee Rose" , "population": 10511100 , "flower picture": "flowers/GA.jpg"}, {"state": "Hawaii" , "capitol": "Honolulu" , "flower": "Yellow Hibiscus/Pua Aloalo" , "population": 1420590 , "flower picture": "flowers/HI.jpg"}, {"state": "Idaho" , "capitol": "Boise" , "flower": "Syringa" , "population": 1750540 , "flower picture": "flowers/ID.jpg"}, {"state": "Illinois" , "capitol": "Springfield", "flower": "Native Violet" , "population": 12723100 , "flower picture": "flowers/IL.jpg"}, {"state": "Indiana" , "capitol": "Indianapolis", "flower": "Peony" , "population": 6695500 , "flower picture": "flowers/IN.jpg"}, {"state": "Iowa" , "capitol": "Des Moines" , "flower": "Wild Rose" , "population": 3148620 , "flower picture": "flowers/IA.jpg"}, {"state": "Kansas" , "capitol": "Topeka" , "flower": "Native Sunflower" , "population": 2911360 , "flower picture": "flowers/KS.jpg"}, {"state": "Kentucky" , "capitol": "Frankfort" , "flower": "Goldenrod" , "population": 4461150 , "flower picture": "flowers/KY.jpg"}, {"state": "Louisiana" , "capitol": "Baton Rouge", "flower": "Magnolia" , "population": 4659690 , "flower picture": "flowers/LA.jpg"}, {"state": "Maine" , "capitol": "Augusta" , "flower": "White Pinecone/Tassel" , "population": 1339060 , "flower picture": "flowers/ME.jpg"}, {"state": "Maryland" , "capitol": "Annapolis" , "flower": "Black-eyed Susan" , "population": 6035800 , "flower picture": "flowers/MD.jpg"}, {"state": "Massachusetts", "capitol": "Boston" , "flower": "Mayflower" , "population": 6882640 , "flower picture": "flowers/MA.jpg"}, {"state": "Michigan" , "capitol": "Lansing" , "flower": "Apple Blossom" , "population": 9984070 , "flower picture": "flowers/MI.jpg"}, {"state": "Minnesota" , "capitol": "Saint Paul" , "flower": "Pink and White Ladys Slipper", "population": 5606250 , "flower picture": "flowers/MN.jpg"}, {"state": "Mississippi" , "capitol": "Jackson" , "flower": "Magnolia" , "population": 2981020 , "flower picture": "flowers/MS.jpg"}, {"state": "Missouri" , "capitol": "Jefferson City", "flower": "Hawthorn" , "population": 6121620 , "flower picture": "flowers/MO.jpg"}, {"state": "Montana" , "capitol": "Helena" , "flower": "Bitterroot" , "population": 1060660 , "flower picture": "flowers/MT.jpg"}, {"state": "Nebraska" , "capitol": "Lincoln" , "flower": "Goldenrod" , "population": 1925610 , "flower picture": "flowers/NE.jpg"}, {"state": "Nevada" , "capitol": "Carson City", "flower": "Sagebrush" , "population": 3027340 , "flower picture": "flowers/NV.jpg"}, {"state": "New Hampshire", "capitol": "Concord", "flower": "Purple Lilac" , "population": 1353460 , "flower picture": "flowers/NH.jpg"}, {"state": "New Jersey" , "capitol": "Trenton" , "flower": "Purple Violet" , "population": 8886020 , "flower picture": "flowers/NJ.jpg"}, {"state": "New Mexico" , "capitol": "Santa Fe" , "flower": "Yucca" , "population": 2092740 , "flower picture": "flowers/NM.jpg"}, {"state": "New York" , "capitol": "Albany" , "flower": "Rose" , "population": 19530400 , "flower picture": "flowers/NY.jpg"}, {"state": "North Carolina", "capitol": "Raleigh" , "flower": "Dogwood" , "population": 10381600 , "flower picture": "flowers/NC.jpg"}, {"state": "North Dakota", "capitol": "Bismarck" , "flower": "Wild Prairie Rose" , "population": 758080 , "flower picture": "flowers/ND.jpg"}, {"state": "Ohio" , "capitol": "Columbus" , "flower": "Scarlet Carnation" , "population": 11676300 , "flower picture": "flowers/OH.jpg"}, {"state": "Oklahoma" , "capitol": "Oklahoma City", "flower": "Mistletoe" , "population": 3940240 , "flower picture": "flowers/OK.jpg"}, {"state": "Oregon" , "capitol": "Salem" , "flower": "Oregon Grape" , "population": 4181890 , "flower picture": "flowers/OR.jpg"}, {"state": "Pennsylvania", "capitol": "Harrisburg" , "flower": "Mountain Laurel" , "population": 12800900 , "flower picture": "flowers/PA.jpg"}, {"state": "Rhode Island", "capitol": "Providence" , "flower": "Violet" , "population": 1058290 , "flower picture": "flowers/RI.jpg"}, {"state": "South Carolina", "capitol": "Columbia" , "flower": "Yellow Jessamine" , "population": 5084160 , "flower picture": "flowers/SC.jpg"}, {"state": "South Dakota", "capitol": "Pierre" , "flower": "Pasqueflower" , "population": 878698 , "flower picture": "flowers/SD.jpg"}, {"state": "Tennessee" , "capitol": "Nashville" , "flower": "Iris" , "population": 6771630 , "flower picture": "flowers/TN.jpg"}, {"state": "Texas" , "capitol": "Austin" , "flower": "Bluebonnet" , "population": 28628700 , "flower picture": "flowers/TX.jpg"}, {"state": "Utah" , "capitol": "Salt Lake City", "flower": "Sego Lily" , "population": 3153550 , "flower picture": "flowers/UT.jpg"}, {"state": "Vermont" , "capitol": "Montpelier" , "flower": "Red Clover" , "population": 624358 , "flower picture": "flowers/VT.jpg"}, {"state": "Virginia" , "capitol": "Richmond" , "flower": "Flowering Dogwood" , "population": 8501290 , "flower picture": "flowers/VA.jpg"}, {"state": "Washington" , "capitol": "Olympia" , "flower": "Western Rhododendron" , "population": 7523870 , "flower picture": "flowers/WA.jpg"}, {"state": "West Virginia", "capitol": "Charleston" , "flower": "Big Rhododendron" , "population": 1804290 , "flower picture": "flowers/WV.jpg"}, {"state": "Wisconsin" , "capitol": "Madison" , "flower": "Wood Violet" , "population": 5807410 , "flower picture": "flowers/WI.jpg"}, {"state": "Wyoming" , "capitol": "Cheyenne" , "flower": "Indian Paintbrush" , "population": 577601 , "flower picture": "flowers/WY.jpg"}, ]
def display_states(): for state in state_data: for i in state: print(i + ":", state[i]) print()
def search_state(): search = input("Enter the state you want to search: ") for state in state_data: if state["state"] == search: for i in state: print(i + ":", state[i]) print() img = mpimg.imread((state["flower picture"])) imgplot = plt.imshow(img) plt.title(state["flower"]) plt.show ()
def display_barchart_top5(): sorted_states = sorted( data, key=lambda i: i['population'], reverse=True) # fig = plt.figure(figsize=(10, 5))
sorted_states_names = [] sorted_states_populations = []
for x in sorted_states[:5]: sorted_states_names.append(x["state"]) sorted_states_populations.append(x["population"])
# creating the bar plot plt.bar(sorted_states_names, sorted_states_populations, width=0.4) plt.show()
def update_population(): state_to_be_modified = input("Enter the state you want to update the population for: ") population = int(input("Enter the population you want to increase: ")) for state in data: if state["state"] == state_to_be_modified: state["population"] += population break
while True: print ( "1. Display all U.S. States in Alphabetical order along with the " "Capital, State Population, and Flower " ) print ( "2. Search for a specific state and display the appropriate Capital " "name, State Population, and") print ( "an image of the associated State Flower." ) print ( "3. Provide a Bar graph of the top 5 populated States showing their " "overall population " ) print ( "4. Update the overall state population for a specific state. " ) print ( "5. Exit the program " ) user_input = int ( input ( "Enter the number of your choice: ") ) # add check for int between 1 and 5 else message and return to menu # if user_input ( 0> or 5< ), then if user_input == 1: display_states () elif user_input == 2: search_state () elif user_input == 3: display_barchart_top5() elif user_input == 4: update_population () elif user_input == 5: #Exits the program print("Thank you for using the State Information Program!") break else: #Checks for valid user input print("That is not a correct entry, please select 1-5")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
