0

I'm doing 'guess the word' on Python and I can't figure out this error.

AttributeError: 'int' object has no attribute 'index' (python)

It is giving me an error on the line letterIndex=word.index(guess).

def checkLetter(word):

    blanks = '_' * len(str(word))
    print('Word: ', blanks)
    if str(guess) == str(letters):
        letterIndex = word.index(guess)
        newBlank = blanks[:letterIndex * 2] + guess + blanks[letterIndex * 2 + 1]
        print('Guess Corrrect')
1
  • 2
    You need to learn to read the error messages. It is telling you what went wrong exactly. word is an integer. Integers do not have the method index. So you go through your code and search for the place where you define word as an integer. Commented Dec 29, 2015 at 23:37

3 Answers 3

1

From earlier when you cast word as a string, I presume that word is not a string. With that in mind, it may not have the index function. At the very least, you likely need to change that line to

letterIndex=str(word).index(guess)

Although I would raise questions on why a variable called word is an int.

Sign up to request clarification or add additional context in comments.

Comments

0

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

The error message could hardly be more clear. It tells you that word is an int and has no attribute named index. Did you intend word to be a string? Or did you forget to convert it to a string?

Comments

0

word variable is of int type , and int type does not have the index function. Convert word to string type using str():

letterIndex=str(word).index(guess)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.