Using Python 3.4.3 https://www.python.org/download/ 1. Create a dictionary named
ID: 3663143 • Letter: U
Question
Using Python 3.4.3 https://www.python.org/download/
1. Create a dictionary named states that uses two-letter state abbreviations as keys. Each value in the states dictionary should be a list holding both the state name and state capital city. Add at least a dozen states to the dictionary, but feel free to add all 50. See example on page 374. Loop through the entire dictionary showing the state abbreviation, state name, and state capital (see sample output). Then, create a quiz using another loop to ask for state capitals. Use the dictionary to formulate the questions and correct answers. Provide feedback to the user after each quiz question.
SAMPLE OUTPUT
GA state is Georgia capital city is Atlanta
CA state is California capital city is Sacramento
NY state is New York capital city is New York
...
States Quiz
What is the capital of Georgia? Atlanta
Correct!
What is the capital of California? Los Angeles
Sorry. The capital is Sacramento
What is the capital of New York? ...
Explanation / Answer
# State Capitals Game
import random
def main():
state_capitals={"Washington":"Olympia","Oregon":"Salem",
"California":"Sacramento","Ohio":"Columbus",
"Nebraska":"Lincoln","Colorado":"Denver",
"Michigan":"Lansing","Massachusetts":"Boston",
"Florida":"Tallahassee","Texas":"Austin",
"Oklahoma":"Oklahoma City","Hawaii":"Honolulu",
"Alaska":"Juneau","Utah":"Salt Lake City",
"New Mexico":"Santa Fe","North Dakota":"Bismarck",
"South Dakota":"Pierre","West Virginia":"Charleston",
"Virginia":"Richmond","New Jersey":"Trenton",
"Minnesota":"Saint Paul","Illinois":"Springfield",
"Indiana":"Indianapolis","Kentucky":"Frankfort",
"Tennessee":"Nashville","Georgia":"Atlanta",
"Alabama":"Montgomery","Mississippi":"Jackson",
"North Carolina":"Raleigh","South Carolina":"Columbia",
"Maine":"Augusta","Vermont":"Montpelier",
"New Hampshire":"Concord","Connecticut":"Hartford",
"Rhode Island":"Providence","Wyoming":"Cheyenne",
"Montana":"Helena","Kansas":"Topeka",
"Iowa":"Des Moines","Pennsylvania":"Harrisburg",
"Maryland":"Annapolis","Missouri":"Jefferson City",
"Arizona":"Phoenix","Nevada":"Carson City",
"New York":"Albany","Wisconsin":"Madison",
"Delaware":"Dover","Idaho":"Boise",
"Arkansas":"Little Rock","Louisiana":"Baton Rouge"}
incorrect_answers=[]
print "Learn your state capitals! "
while len(state_capitals)>0:
choice=random.choice(state_capitals.keys())
correct_answer=state_capitals.get(choice)
print "What is the capital city of",choice,"?"
answer=raw_input("# ")
if answer.lower()==correct_answer.lower():
print "That's Correct! "
del state_capitals[choice]
else:
print "That's Incorrect."
print "The correct answer is",correct_answer
incorrect_answers.append(choice)
print "You missed",len(incorrect_answers),"states. "
if incorrect_answers:
print "here's the ones that you may want to brush up on: "
for each in incorrect_answers:
print each
else:
print "Perfect!"
response=""
while response<>"n":
main()
response=raw_input(" Play again?(y/n) # ")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.