-3

if guess in word:

    if guess == word[0] and 1 >= len(word):
        print("Your guess is correct. The letter of the word you guess has been revealed. ")
        right[0] = word[0]
        print(' '.join(right))
        score +=1
    if guess == word[1] and 2 >= len(word):
        print("Your guess is correct. The letter of the word you guess has been revealed. ")
        right[1] = word[1]
        print(' '.join(right))
        score +=1
        print(score)
    if guess == word[2] and 3 >= len(word):
        print("Your guess is correct. The letter of the word you guess has been revealed. ")
        right[2] = word[2]
        print(' '.join(right))
        score +=1
        print(score)
    if guess == word[3] and 4 >= len(word):
        print("Your guess is correct. The letter of the word you guess has been revealed. ")
        right[3] = word[3]
        print(' '.join(right))
        score +=1
        print(score)
    if guess == word[4] and 5 >= len(word):
        print("Your guess is correct. The letter of the word you guess has been revealed. ")
        right[4] = word[4]
        print(' '.join(right))
        score +=1
        print(score)

The error is: if guess == word[1] and 2 >= len(word): IndexError: string index out of range

I think the problem is with the and in the if statement but I dont know.

rioV8
  • 24,506
  • 3
  • 32
  • 49
User6510
  • 13
  • 2
  • 2
    You should first check the length and then access the element. – Thomas Sablik Oct 09 '20 at 22:00
  • Do you mean to write `if len(word) > 1 and word[1]==guess` ? Because what you have is not that at all. Not only are you checking conditions in the wrong order; you're checking the wrong conditions. – khelwood Oct 09 '20 at 22:01
  • @User6510 Why did you edited your code back to an incorrect formatting? – Ehsan Oct 09 '20 at 22:02
  • Sorry if my code is confusing. I'm new to python and trying to learn. The project I am going for is hangman – User6510 Oct 09 '20 at 22:03
  • This behavior is called short-circuiting. This question has more info: [Does Python support short-circuiting?](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) – Pranav Hosangadi Oct 09 '20 at 22:07

1 Answers1

0

You should first check the length and then access the element. Additionally the size check is wrong. Change

if guess == word[1] and 2 >= len(word):

to

if len(word) >= 2 and guess == word[1]:
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62