In: Computer Science
How to do this in Python (using Lists):
Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird.
You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions:
1. Display all U.S. States in Alphabetical order along with Capital and Bird
2. Search for a specific state and display the appropriate Capital and Bird
3. Update a Bird for a specific state
4. Exit the program
class state: def __init__(self, state_name, capital, bird): self.state_name = state_name self.capital = capital self.bird = bird def update_bird(self, bird): self.bird = bird def update_bird_for_State(state_list, state_name, bird_name): for state in state_list: if state.state_name == state_name: state.update_bird(bird_name) def display_sorted_states(state_list): for state in sorted([state.state_name for state in state_list]): for state_data in state_list: if state == state_data.state_name: print("State name : ", state_data.state_name, " , Capital : ", state_data.capital, " , Bird : ", state_data.bird) def display_data(state_list, state): data_found = False for state_data in state_list: if state_data.state_name == state: data_found = True print("State name : ", state_data.state_name, " , Capital : ", state_data.capital, " , Bird : ", state_data.bird) if data_found == False: print("No data found.") if __name__ == "__main__": state_list = [] s = state('Colorado','Denver' , 'Lark Bunting') state_list.append(s) s = state('Florida', 'Tallahassee', 'Mockingbird') state_list.append(s) s = state('Georgia', 'Atlanta', 'Brown Thrasher') state_list.append(s) s = state('Indiana', 'Indianapolis', 'Cardinal') state_list.append(s) s = state('Massachusettes', 'Boston', 'Chickadee') state_list.append(s) user_input = 1 while(user_input in [1,2,3,4]): print("\n1. Display all U.S. States in Alphabetical order along with Capital and Bird") print("2. Search for a specific state and display the appropriate Capital and Bird") print("3. Update a Bird for a specific state") print("4. Exit the program") user_input = int(input("\nEnter choices : ")) if(user_input == 1): display_sorted_states(state_list) elif(user_input ==2 ): state_name = input("\nEnter State Name: ") display_data(state_list, state_name) elif(user_input == 3): state_name = input("\nEnter State Name: ") bird_name = input("\nEnter Bird Name: ") update_bird_for_State(state_list, state_name, bird_name) elif(user_input == 4 ): break