Search code examples
pythonattributeerrornonetype

AttributeError: 'NoneType' object has no attribute 'index'


I am trying to make a US Capitals game for my sister, but when I do the following code:

    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.')

I get this error:

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'

What does this mean and what can I do to fix this?


Solution

  • Python is trying to tell you that when you got to line 18, allChoices is None and None objects don't have an index method. The reason for this is because random.shuffle shuffles the list in place and returns None.

    The fix is to change:

    allChoices = random.shuffle(allChoices)
    

    to:

    random.shuffle(allChoices)