Python-字典状态和资本游戏

标签 python dictionary

我创建了一个猜美国各州和首都的游戏。它适用于 Jupyter Notebook。当我将代码保存到 game.py 并在 Windows CMD 中运行时,我的代码不会循环 10 次,而是循环一次,然后 CMD 将自动关闭。有什么理由吗?

还有,我用random.choice函数随机选择了10个州,如何避免重复?每次运行我的代码时,我都需要 10 个独立的状态。

谢谢!

import random

capital_dic={
    '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',
    'Illinios': 'Springfield',
    'Indiana': 'Indianapolis',
    'Iowa': 'Des Monies',
    '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',
    'Neveda': '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',
    'Rhoda Island': 'Providence',
    'South Carolina': 'Columbia',
    'South Dakoda': 'Pierre',
    'Tennessee': 'Nashville',
    'Texas': 'Austin',
    'Utah': 'Salt Lake City',
    'Vermont': 'Montpelier',
    'Virginia': 'Richmond',
    'Washington': 'Olympia',
    'West Virginia': 'Charleston',
    'Wisconsin': 'Madison',
    'Wyoming': 'Cheyenne'  
} # create a dictionary, key is the state and value is the capital
States=list(capital_dic.keys())
print ('Let\'s learn US States and Capitals. 10 rounds. Enter exit to quit the game.')
point=0 # this is the score
for i in range(10):
    state=random.choice(States) # randomly select 10 states, how do I avoid duplicate?
    capital = capital_dic[state]
    user_guess = input('what is the capital of %s?'%state )
    if user_guess.lower() == 'exit':  #if a user type in exit, the game exits
        break
    elif user_guess.title() == capital:
        point+=1
        print('Correct! Your score is %d' %point)
    else:
        print('Incorrect. The capital of {} is {}.'.format(state,capital))
print('We are done. Your final score is %d, thank you.' %point)

最佳答案

您可以使用random.sample 来获取一组唯一的随机名称。

例如:

States=list(capital_dic.keys())
print random.sample(States, 10)

输出:

['Michigan', 'Alabama', 'Virginia', 'Massachusetts', 'Rhoda Island', 'Kentucky', 'Louisiana', 'Wyoming', 'Arizona', 'Vermont']

片段更新:

for state in random.sample(States, 10):
    capital = capital_dic[state]

关于Python-字典状态和资本游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49193164/

相关文章:

python - 如何使用 boto 以编程方式获取 Amazon S3 文件的 MD5 校验和

Python 通过写入 stdin 取消 raw_input/input?

python - “|” 符号在 Python 中是什么意思?

java - Struts 2 - 在 map 内的 map 中保存值

python - JES中如何将图片的一部分做成灰度?

python - 从上三角获取矩阵的索引

python - 如何删除列表中重复的 "keys"并计算值的平均值

c# - 字典 ArgumentException 日志重复键 : which is more performant?

python - 从嵌套字典列表中删除重复值

java - 评估后检索使用的常规变量