我正在尝试为我姐姐制作美国首都游戏,但是当我执行以下代码时:
import random
allUSStates = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
allUSCapitals = {'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', 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas': 'Topeka', 'Kentucky': 'Frankfurt', 'Louisiana': 'Baton Rouge', 'Maine': 'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan': 'Lansing', 'Minnesota': 'St. Paul', 'Mississippi': 'Jackson', 'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada': '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', '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', 'Washington': 'Olympia', 'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
states = allUSStates
numwrong = 0
while len(states) > 0:
state = random.choice(states)
capital = allUSCapitals[random.choice(states)]
print('What is the capital of %s?' % state)
choice1 = allUSCapitals[state]
choice2 = choice1
choice3 = choice1
while choice2 == choice1 or choice3 == choice1 or choice2 == choice3:
choice2 = allUSCapitals[random.choice(allUSStates)]
choice3 = allUSCapitals[random.choice(allUSStates)]
allChoices = [choice1, choice2, choice3]
allChoices = random.shuffle(allChoices)
correctAnswer = allChoices.index(choice1)
print(' 1. ' + allChoices[0] + ' \n 2. ' + allChoices[1] + ' \n 3. ' + allChoices[2] + ' \n')
answer = int(float(input())) - 1
if answer == correctAnswer:
print('\nGood job!')
else:
print('Nope! The answer is ' + choice1 + '!')
numwrong += 1
del states[states.index(choice1)]
percentwrong = (numwrong/50)*100
print('\n\n\n\nYou got ' + numwrong + ' wrong out of 50, or ' + percentwrong + '% wrong.')
我收到此错误:
Traceback (most recent call last):
File "C:\Python32\USStates.py", line 18, in module
correctAnswer = allChoices.index(choice1)
AttributeError: 'NoneType' object has no attribute 'index'
这意味着什么,我该怎么做才能解决这个问题?
答案 0 :(得分:5)
Python试图告诉您,当您到达第18行时,allChoices
为None
且None
个对象没有index
方法。这样做的原因是random.shuffle
将列表随机播放并返回None
。
修复方法是改变:
allChoices = random.shuffle(allChoices)
为:
random.shuffle(allChoices)