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

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'
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...
*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.
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)) = []
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to their squares. Assign the dictionary to the variable squares b. Given the list value_list, assign the number of nonduplicate values to the variable distinct_values
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT