我将如何随机化这个游戏的问题?

时间:2017-11-27 05:08:12

标签: python

对于家庭作业,我试图制作一个游戏,在每个州的首都测验用户,有点像抽认卡游戏。我已经满足了以下代码对程序的所有要求,除非他们希望问题是随机排列的。我该如何改变字典呢? 我知道如何改变列表而不是字典,因为我认为它们已经被认为是随机顺序..但我按照相同的顺序得到问题我输入了键/值(按字母顺序排列)

    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 个答案:

答案 0 :(得分:1)

使用random.shuffle

from random import shuffle
states = flashcards.keys()
shuffle(states)
for state in states:
    print 'State: {}, Capital: {}'.format(state, flashcards[state])