Write a program that creates a dictionary containing the U.S. states as keys, and their capitals as values, by reading data from the provided state_capitals.txt file. The program should then randomly quiz the user by displaying the name of a state and asking the user to enter that state’s capital. The program should keep a count of the number of correct and incorrect responses. Answers should not be case sensitive. When the user enters 0, the quiz should stops and report the results.

Answer :

temmydbrain

Answer:

Check the attached image below to see the screenshot

Explanation:

# Create a dictionary of US states as key and capitals as values

  1. capitals_dict = {"Alabama": "Montgomery", "Alaska": "Juneau", "Arizona":                    "Phoenix",
  2. "Arkansas": "Little Rock", "California": "Sacramento", "Colorado": "Denver",
  3. "Connecticut": "Hartford", "Delaware": "Dover", "Florida": "Tallahassee",
  4. "Georgia": "Atlanta", "Hawaii": "Honolulu", "Idaho": "Boise",
  5. "Illinois": "Springfield", "Indiana": "Indianapolis", "Iowa": "Des Moines",
  6. "Kansas": "Topeka", "Kentucky": "Frankfort", "Louisiana": "Baton Rouge",
  7. "Maine": "Augusta", "Maryland": "Annapolis", "Massachusetts": "Boston",
  8. "Michigan": "Lansing", "Minnesota": "St. Paul", "Mississippi": "Jackson",
  9. "Missouri": "Jefferson City", "Montana": "Helena", "Nebraska": "Lincoln",
  10. "Nevada": "Carson City", "New Hampshire": "Concord", "New Jersey": "Trenton",
  11. "New Mexico": "Santa Fe", "New York": "Albany", "North Carolina": "Raleigh",
  12. "North Dakota": "Bismarck", "Ohio": "Columbus", "Oklahoma": "Oklahoma City",
  13. "Oregon": "Salem", "Pennsylvania": "Harrisburg", "Rhode Island": "Providence",
  14. "South Carolina": "Columbia", "South Dakota": "Pierre",
  15. "Tennessee": "Nashville", "Texas": "Austin", "Utah": "Salt Lake City",
  16. "Vermont": "Montpelier", "Virginia": "Richmond", "Washington": "Olympia",
  17. "West Virginia": "Charleston", "Wisconsin": "Madison", "Wyoming": "Cheyenne"}

# Number of questions to be asked in quiz

# You can change it accordingly

numberOfQuestion = 10

# Number of correct responses by user

correctResponse = 0

# Number of correct responses by user

incorrectResponse = 0

# Counter to keep track of number of questions asked

i = 0

# List of the states already asked

list_capitals_asked = []

while i < numberOfQuestion:

   i += 1  # Increment i by 1

   # Select a state randomly from dictionary

   state = random.choice(list(capitals_dict.keys()))

   # Check if list already contains this state

   # If yes, select another state

   # Keep doing that, until a new state is found

   while list_capitals_asked.__contains__(state):

       state = random.choice(list(capitals_dict.keys()))

   # Add state to list

   list_capitals_asked.append(state)

   # Get capital of state

   correct_ans = capitals_dict[state]

   # Ask user to enter capital of state

   user_ans = input('What is the capital of ' + state + '? ')

   # If user answer's matches correct answer

   # Increment correctResponse by 1

   # Else, Increment incorrectResponse by 1

   if user_ans == correct_ans:

       correctResponse += 1

   else:

       incorrectResponse += 1

# Print correct and incorrect responses

print('Correct Responses:', correctResponse)

print('Incorrect Responses:', incorrectResponse)

${teks-lihat-gambar} temmydbrain
${teks-lihat-gambar} temmydbrain
${teks-lihat-gambar} temmydbrain

Other Questions