0

for a homework assignment, i'm trying to make a game that quizzes users on the capitals of each state, kind of like a flashcards game. I've satisfied all requirements for the program with the code below except they want the questions to be in a random order. how can i shuffle the dictionary? I know how to shuffle a list but not a dictionary, as i thought they were already supposed to be in a random order.. yet i get the questions in the same order i typed the keys/values (alphabetical by state)

    flashcards = {'ALABAMA': 'MONTGOMERY',
              'ALASKA': 'JENEAU',
              'ARIZONA': 'PHOENIX',
              'ARKANSAS': 'LITTLE ROCK',
              'CALIFORNIA': 'SACRAMENTO',
              'COLORADO': 'DENVER',
              'CONNECTICUT': 'HARTFORD',
              'DELAWARE': 'DOVER',
              'FLORIDA': 'TALLAHASSEE',
              'GEORGIA': 'ATLANTA',
              'HAWAII': 'HONOLULU',
              'IDAHO': 'BOISE',
              'ILLINOIS': 'SPRINGFIELD',
              'INDANA': 'INDIANAPOLIS',
              'IOWA': 'DES MOINES',
              'KANSAS': 'TOPEKA',
              'KENTUCKY': 'FRANKFORT',
              'LOUISIANA': 'BATON ROUGE',
              'MAINE': 'AUGUSTA',
              'MARYLAND': 'ANNAPOLIS',
              'MASSACHUSETTS': 'BOSTON',
              'MICHIGAN': 'LANSING',
              'MINNESOTA': 'ST. PAUL',
              'MISSISSIPPI': 'JACKSON',
              'MISSOURI': 'JEFFERSON CITY',
              'MONTANA': 'HELENA',
              'NEBRASKA': 'LINCOLN',
              'NAVADA': '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',
              'WASHINTON': 'OLYMPIA',
              'WEST VIRGINIA': 'CHARLESTON',
              'WISCONSIN': 'MADISON',
              'WYOMING': 'CHEYENNE'}


def main():
    incorrect = 0
    correct = 0
    print('Let\'s play the State\'s game!!')
    for b in flashcards.keys():
        question = input('What is the capital of ' + b +'? : ')
        if question.upper() == flashcards[b].upper():
            correct += 1
            print('correct!!')
            print('Correct: ', correct)
            print('Incorrect: ', incorrect)
        else:
            incorrect += 1
            print('oops! that is incorrect')
            print('Correct: ', correct)
            print('Incorrect: ', incorrect)


main()
  • 1
    Dictionary keys are already in random order. Why shuffle then? – Arunesh Singh Nov 27 '17 at 05:11
  • yeah thats what i thought too.. but the code as written above is giving me the questions in alphabetical order by state.. – Elijah Ryker Nov 27 '17 at 05:14
  • 2
    @AruneshSingh: dictionary keys are in an _arbitrary_ order, one stable unless the dictionary is mutated; that's not the same as random. – DSM Nov 27 '17 at 05:23
  • [Aside: not relevant to the issue at hand, but there are many spelling errors in this dictionary.] – DSM Nov 27 '17 at 05:26

1 Answers1

1

Use random.shuffle

from random import shuffle
states = flashcards.keys()
shuffle(states)
for state in states:
    print 'State: {}, Capital: {}'.format(state, flashcards[state])
Chrispresso
  • 3,660
  • 2
  • 19
  • 31