Question

In: Computer Science

How to do this in Python (using Lists): Create a python program that allows a user...

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

Solutions

Expert Solution

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


Related Solutions

Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
Create a simple python app that allows the user to create a roster of students and...
Create a simple python app that allows the user to create a roster of students and their grade on CUS-1166. Moreover the app needs to calculate the average grade of students added to the roster. 1-To begin with, create a new file n the same working folder as part A (i.e. cus1166_lab1) and name it app.py. Moreover, create a subfolder and name it mymodules. 2-Within mymodules create the files __init__.py , models.py , math_utils.py . 3-In the models.py file define...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
9) This is in Python Code a program that allows the user to manage its expenses...
9) This is in Python Code a program that allows the user to manage its expenses accepting the following commands: - Add <player_name> -> This command adds a new player to the system. Assume there can’t be repeated names. If a name already exists then an error is shown to the user. - Score <player_name> <score> -> This command adds the score to the player with the name player-name. Each score needs to be added individually - Report -> This...
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT