【问题标题】:Python-Dictionary states and capital gamePython-字典状态和资本博弈
【发布时间】:2018-08-17 23:45:44
【问题描述】:

我创建了一个游戏来猜美国各州和首都。它适用于 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_states = random.sample(States, 10) 选择10 个随机状态。我不知道你的代码不循环是什么意思:它确实循环。

标签: python dictionary


【解决方案1】:

您可以使用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]

【讨论】:

  • 我应该放弃 for 循环,只使用 random.sample(States, 10) 吗?
  • 您可以更新循环以遍历 random.sample 列表
  • 太棒了!让我试试。谢谢!
【解决方案2】:

您的循环问题是您在 Windows cmd 中使用了python2input 函数与python3 版本有点不同,所以如果你想用python2 运行这段代码,请尝试使用raw_input 函数:

user_guess = raw_input('what is the capital of %s?' % state)

详情请见this answer

【讨论】:

  • 是的,我明白了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
相关资源
最近更新 更多