Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: Write a program that creates a dictionary containing the US states as keys and their capitals as values. The program should randomly quiz the

Python:

Write a program that creates a dictionary containing the US states as keys and their capitals as values.

The program should randomly quiz the user by displaying the name of a state and asking the user to enter that states capital. It should keep a count of the number of correct and incorrect responses and let the user know how they are doing. It should not penalize the user for capitalization errors. It should also not ask a user about a state if they have already correctly identified the capital (if they have not, it can ask them again). If, instead of guessing, the user types 0, the game should quit and tell them their score.

#####

Example program outputs:

What is the capital of Illinois? (or enter 0 to quit): springfield

That is correct.

What is the capital of Pennsylvania? (or enter 0 to quit): Pittsburgh

That is incorrect. It is Harrisburg.

What is the capital of North Dakota? (or enter 0 to quit): bismarck

That is correct.

What is the capital of Nevada? (or enter 0 to quit): Las Vegas

That is incorrect. It is Carson City.

What is the capital of Iowa? (or enter 0 to quit): 0

You had 2 correct responses and 2 incorrect responses.

######

I have the dictionary premade work here, please keep working on the rest of coding work:

import random

def main(): # Initialize dictionary, key is the state and value is the capital capitals = { 'Alabama' : 'Montgomery', 'Alaska' : 'Juneau', 'Arizona' : 'Phoenix', 'Arkansas' : 'Little Rock', 'California' : 'Sacramento', 'Colorado' : 'Denver', 'Connecticut' : 'Hartford', 'Delaware' : 'Dover', 'Florida' : 'Tallahassee', 'Georgia' : 'Atlanta', 'Hawaii' : 'Honolulu', 'Idaho' : 'Boise', 'Illinois' : 'Springfield', 'Indiana' : 'Indianapolis', 'Iowa' : 'Des Moines', 'Kansas' : 'Topeka', 'Kentucky' : 'Frankfort', 'Louisiana' : 'Baton Rouge', 'Maine' : 'Augusta', 'Maryland' : 'Annapolis', 'Massachusetts' : 'Boston', 'Michigan' : 'Lansing', 'Minnesota' : 'Saint Paul', 'Mississippi' : 'Jackson', 'Missouri' : 'Jefferson City', 'Montana' : 'Helena', 'Nebraska' : 'Lincoln', 'Nevada' : 'Carson City', 'New Hampshire' : 'Concord', 'New Jersey' : 'Trenton', 'New Mexico' : 'Santa Fe', 'New York' : 'Albany', 'North Carolina' : 'Raleigh', 'North Dakota' : 'Bismarck', 'Ohio' : 'Columbus', 'Oklahoma' : 'Oklahoma City', 'Oregon' : 'Salem', 'Pennsylvania' : 'Harrisburg', 'Rhode Island' : 'Providence', 'South Carolina' : 'Columbia', 'South Dakota' : 'Pierre', 'Tennessee' : 'Nashville', 'Texas' : 'Austin', 'Utah' : 'Salt Lake City', 'Vermont' : 'Montpelier', 'Virginia' : 'Richmond', 'Washington' : 'Olympia', 'West Virginia' : 'Charleston', 'Wisconsin' : 'Madison', 'Wyoming' : 'Cheyenne' }

correct = 0 incorrect = 0 next_question = True index = 0 current_state ='' user_solution = '' #capitals = {} while next_question: # get access to the list of state names state_lists = list(capitals.keys())

current_state = get_random_state(state_lists) capital = capitals[current_state]

def get_random_state(states): index = (random.randint(1,50)-1) states = def user_solution(): user_solution = input('what is the capital of' + current_state + '?(or enter 0 to quit): ') if user_solution.lower() == '0': next_question = False

elif user_solution.lower() == capital: correct += 1 print('That is correct.') else: incorrect += 1 print('That is incorrect. It is ' + capitals(current_state)) print("You had " + correct + " correct responses and " + incorrect + " incorrect response")

if __name__ == '__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions