Home > other >  Trying to work out how to call a specific set of indexes when a specific state is chosen within 3 li
Trying to work out how to call a specific set of indexes when a specific state is chosen within 3 li

Time:06-06

Hey everyone hope all is well. I'm still learning and working on this. I know I have it set up a little wonky. But what I am trying to do is have my findState() function take the users input, check if the state they inputted is inside the state list, then print the capital and population along with that state. What I am trying to figure out is how to call the 3 lists in my stateInfo() function into my findState() function and also print the same index for population and capital as the state that was inputted.

   import itertools



    def menu():
        print("Welcome to my state altering program!\n"
          "[1] Display all the states in alphabetical order with all info.\n"
          "[2] Find a specific state and print all that states info.\n"
          "[3] Create a graph of the 5 most populated states.\n"
          "[4] Update a chosen states population.\n"
          "[5] Exit\n")

    def stateInfo():
    state = ["Alabama", "Idaho", "Missouri", "North Dakota", "Louisiana", "South Carolina", "New     Mexico", "Massachusetts",
            "Illinois", "Georgia", "Colorado", "North Carolina", "Connecticut", "Alaska", "Minnesota", "Nevada",
            "West Virginia", "Arkansas", "Florida", "New Hampshire", "Maryland", "Pennsylvania", "Oklahoma",
            "Mississippi", "Tennessee", "Michigan", "Vermont", "Rhode Island", "Washington", "South Dakota",
            "Kansas", "Oregon", "Kentucky", "Hawaii", "California", "Arizona", "Delaware", "Indiana",
            "Maine", "Wisconsin", "Nebraska", "Iowa", "New Jersey", "Virginia", "Wyoming", "Ohio", "Texas",
            "Utah", "New York", "Montana"]

    print("Here are the states:")
    print(str(state)   "\n")
    print("Here are the states in order:")
    state.sort()
    print(str(state)   "\n")


    capital = ["Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta",
               "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis",
               "Boston", "Lansing", "Saint Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Trenton",
               "Santa Fe", "Albany", "Raleigh", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Bismarck",
               "Pierre", "Nashville", "Austin", "Salt Lake City", "Nashville", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison",
               "Cheyenne"]
    print("Here are all the states capitals in order of state, Ex:(Montgomery, Alabama):")
    print(str(capital)   "\n")

    population = [4918689, 727951, 7399410, 3025875, 39562858, 5826185, 3559054, 982049, 21711157, 10723715, 1411151, 1823594, 12620571,
                  6768941, 3161522, 2915269, 4474193, 4637898, 1349367, 6055558, 6902371, 9989642, 5673015, 2971278, 6153233, 1076891,
                  1943202, 3132971, 1365957, 8878355, 2100917, 19376771, 41594553, 766044, 11701859, 3973707, 4253588, 12803056, 1060435,
                  5213272, 890620, 6886717, 29363096, 3258366, 623620, 8569752, 7705917, 1780003, 5837462, 579917]

    for (a, b, c) in zip(state, capital, population):
        print("State: "   a   ", Capital: "   b   ", Population: "   str(c)   "\n")
    return state, capital, population



def findState():
    stateList = stateInfo()
    stateSearch = input("Which state would you like information for?\n")
    if stateSearch in stateList:
        print(stateSearch)
        print(stateList.index(stateSearch))
        stateInfo.capital
        print(stateInfo.capital)
        print(stateInfo.population[stateSearch])



menu()
selection = int(input("Please make a selection.\n"))
while selection != 5:
    if selection == 1:
        stateInfo()
    elif selection == 2:
        findState()
    elif selection == 3:
        print("Not done\n")
    elif selection == 4:
        print("Not done\n")

    menu()
    selection = int(input("Would you like to make another selection?\n"))
    if selection == 5:
        print("Goodbye and thank you for using my program for world domination!")

CodePudding user response:

Your stateInfo() function currently returns a tuple of lists.

Without rewriting things too much, can I propose the below and see if it answers your needs?

First, amend the return of your stateInfo() function to:

#return state, capital, population
return {a: (b, c) for (a, b, c) in zip(state, capital, population)}

and then update findState() to:

def findState():
    stateList = stateInfo()
    stateSearch = input("Which state would you like information for?\n")
    if stateSearch in stateList:
        print(f'You have searched for {stateSearch}')
        print(f'The capital of {stateSearch} is {stateList[stateSearch][0]}')
        print(f'The population of {stateSearch} is {stateList[stateSearch][1]}')

FYI, this is what stateList looks like now:enter image description here

CodePudding user response:

Maintaining and aligning 3 lists is not ideal. Better to have a dictionary (JSON on disk). Here's a fragment of a JSON file with a suggestion for an appropriate structure:

{
  "Alabama": {
    "capital": "Montgomery",
    "population": 4874747
  },
  "Alaska": {
    "capital": "Juneau",
    "population": 739795
  },
  "Arizona": {
    "capital": "Phoenix",
    "population": 7016270
  }, # and so on

Then you have a simpler structure to the program. Something like this:

import json

with open('USA.json') as j:
    USA = json.load(j)

while True:
    print("\nWelcome to my state altering program!\n"
          "[1] Display all the states in alphabetical order with all info.\n"
          "[2] Find a specific state and print all that states info.\n"
          "[3] Create a graph of the 5 most populated states.\n"
          "[4] Update a chosen states population.\n"
          "[5] Exit\n")
    match input('Select a function: '):
        case '1':
            for state in sorted(USA):
                capital = USA[state]['capital']
                population = USA[state]['population']
                print(f'{state}, {capital}, {population:,}')
        case '2':
            state = input('Select state name: ')
            data = USA.get(state)
            population = data['population'] if data else -1
            capital = data['capital'] if data else 'n/a'
            print(f'{state}, {capital}, {population:,}')
        case '3' | '4':
            pass  # not yet implemented
        case '5':
            break
        case _:
            print('Invalid selection')
  • Related