Question

In: Computer Science

Python - Create/Initialize a dictionary with 5 state/capital pairs of your choosing. The state is the...

Python

- Create/Initialize a dictionary with 5 state/capital pairs of your choosing. The state is the key and the capital is the value (e.g. “Kansas”:”Topeka”). Only one statement is needed to do this.

- Then prompt the user for a state.

e.g. The output

Enter a state, and I will display the capital.

State: Colorado

Colorado's capital is Denver.

- If that state exists in the dictionary, display the capital, else display "I'm sorry, but I still need to record " followed by the state entered.

e.g. the output

Enter a state, and I will display the capital.

State: Nebraska

I'm sorry, but I still need to record Nebraska.

- Modify the else to also include the ability to add the state and capital to the dictionary.

e.g. the output

Enter a state, and I will display the capital.

State: Nebraska

I'm sorry, but I still need to record Nebraska.

What is Nebraska's capital? Lincoln

The recorded states and capitals thus far are:

{'Kansas': 'Topeka', 'Iowa': 'Des Moines', 'Michigan': 'Lansing', 'California': 'Sacramento', 'Colorado': 'Denver', 'Nebraska': 'Lincoln'}

Solutions

Expert Solution

Hey, i tried to solve the problem as you have discussed.

I hope my answer will help you.

If you need any more help with the solution comment here and i will be happy to help you

thanks.

Here is the solution


stateCapitals = {
  "utah": "Salt Lake City",
  "Arizona": "Pheonix",
  "California": "Sacramento",
  "Georgia" :"Atlanta",
  "indiana":"Indianapolis"
}
print('Enter a state, and I will display the capital')
state = input()
x = stateCapitals.get(state)
if x==None:
    print("I'm sorry, but I still need to record",state)
    print('What is',state,"'s capital")
    capital = input()
    stateCapitals[state]=capital
    print("The recorded states and capitals thus far are:\n",stateCapitals)
else :
    print(state,"'s capital is",x)
    


stateCapitals = {
"utah": "Salt Lake City",
"Arizona": "Pheonix",
"California": "Sacramento",
"Georgia" :"Atlanta",
"indiana":"Indianapolis"
}
print('Enter a state, and I will display the capital')
state = input()
x = stateCapitals.get(state)
if x==None:
print("I'm sorry, but I still need to record",state)
print('What is',state,"'s capital")
capital = input()
stateCapitals[state]=capital
print("The recorded states and capitals thus far are:\n",stateCapitals)
else :
print(state,"'s capital is",x)
  


Related Solutions

In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...
In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair to the dictionary. Change one of the values through reassignment. Access at least 3 values using the keys. Access at least 3 keys using the values. Delete an element. Test for key inclusion for 3 elements. Loop through the dictionary, printing out the key and value on the same line separated by a space. Key1, value1 Key2, value2 …,     … (Please if you...
In Perl: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...
In Perl: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair to the dictionary. Change one of the values through reassignment. Access at least 3 values using the keys. Access at least 3 keys using the values. Delete an element. Test for key inclusion for 3 elements. Loop through the dictionary, printing out the key and value on the same line separated by a space. Key1, value1 Key2, value2 …,     … (Please if you...
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names to addresses. A person's name (stored as a string) will be the KEY and that person's address (stored as a string) will be the VALUE. 1.2) Insert into the dictionary 'addresses' the names and addresses of two (possibly imaginary) friends of yours. 1.3) Create a second empty dictionary called 'ages'. The dictionary you just created will map names to ages. A person's name (stored...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Create an object call Accounts using python for a banking system. Initialize the account with three...
Create an object call Accounts using python for a banking system. Initialize the account with three data as inputs : Firstname, Lastname and initial deposit. Create 4 additional member functions: Deposit, Withdraw, Fee Calculations, interest The fee calculation is going to be $14 per month if the total amount in the account is less than $1000. Interest is set to be at 5%.
python Create a dictionary and insert several English words as keys and the Pig Latin (or...
python Create a dictionary and insert several English words as keys and the Pig Latin (or any other language) translations as values. Write a function called bonus that takes as a parameter a dictionary that has names as keys and salaries as values. Your function should increase everyone’s salary in the dictionary by 5%. Write a function called updateAge that takes as parameters a list of names of people whose birthday it is today, and a dictionary that has names...
Python create a function tryhard and print out the dictionary as a string form. For example...
Python create a function tryhard and print out the dictionary as a string form. For example def tryhard(d:dict): #Code here input: d = {'first': {}, 'second': {'1': {'move'}, '0': {'move', 'slow'}}, 'third': {'1': {'stop'}}} output = " first movie: [ ]\n third movie: [('1', ['stop'])]\n second movie: [('0', ['slow', 'move']), ('1', ['move'])]\n"
python pls create a function party_place: that search the dictionary and figure out where they party...
python pls create a function party_place: that search the dictionary and figure out where they party in that day. For example def party_place(dict2: dict, date: (int,int,int)): dict1= {'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output print(party_place(dict1, (2000,5,20,720)) = ['fire1', 'fire2'] print(party_place(dict1, (2000,5,21,720)) = ['fire1'] print(party_place(dict1, (2000,5,22,720)) = []
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT