【发布时间】: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