CoCalc -- Collaborative Calculation in the Cloud
Sharedsage_worksheets / StatesQuiz.ipynbOpen in CoCalc

Worksheets related to Applied Discrete Structures

State Capitals Quiz

import csv
with open('States.csv') as csvDataFile:
    csvData = csv.reader(csvDataFile)
    for row in csvData:
        print([row[0],row[2]])
['name', 'capital'] ['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']
csvArray=[]
with open('States.csv') as csvDataFile:
    csvData = csv.reader(csvDataFile)
    for row in csvData:
        csvArray=csvArray+[row]
for row in csvArray:
    print([row[0],row[2]])
['name', 'capital'] ['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']
csvArray[3]
['ARIZONA', 'AZ', 'Phoenix', 'Phoenix', '6,595,778', '114,006', 'MT (UTC-07)', '', 'NO']

The way the list of states is created next is similar to "set-builder notation" in mathematics. After creating the list, we display only the first five. For any list, L, L[1:] is all but the first item in the list. For more about this notation: look here. We use slicing again to show the first five entries (entries 0 through 4) of the result with states[:5]

states=[row[0] for row in csvArray[1:]]
states[:5]
['ALABAMA', 'ALASKA', 'ARIZONA', 'ARKANSAS', 'CALIFORNIA']
capitals=[row[2] for row in csvArray[1:]]
capital={}
for k in range(50):
    capital[states[k]]=capitals[k]
capital['UTAH']
'Salt Lake City'
import random
def question():
    s=random.sample(states,4)
    q=random.sample(Set(s),1)
    return [q[0],map(lambda state:capital[state],s)]
question()
['GEORGIA', ['Atlanta', 'Columbia', 'Raleigh', 'Augusta']]
def quiz(n):
    q=[]
    for k in range(n):
        q=q+[question()]
    return q
q1=quiz(2)
q1
[['NORTH DAKOTA', ['Augusta', 'Albany', 'Helena', 'Bismarck']], ['WASHINGTON', ['Olympia', 'Boston', 'Columbia', 'Concord']]]
for quest in q1:
    raw_input('capital of '+str(quest[0])+':      '+str(quest[1]))
capital of NORTH DAKOTA: ['Augusta', 'Albany', 'Helena', 'Bismarck']
capital of WASHINGTON: ['Olympia', 'Boston', 'Columbia', 'Concord']
response=raw_input('what is your name')
response
what is your name
'ken'