Python Forum
Invalid syntax: string index out of range
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid syntax: string index out of range
#1
import time
import sys

# introduction to  the game

print("You are playing the guessing game! To win you must guess the word, within the 5 lives you are given. If you use all 5 lives, you lose!")

time.sleep(2)

print("Type 'yes' when your ready to play")

time.sleep(0.5)


#behind the scenes stuff

word = 'dog'
lives = 5
win = 0

  




# game

ans = input("Ready: ")

while ans == 'yes':
  user = input("Please guess a letter: ")
  if user == word[1] or word[3] or word[2]:
    print("Well done! You guessed a letter correctly!")
    win = win + 1
    time.sleep(1)
    print("You have", lives, "lives!")
    time.sleep(2)
    
    
    if user != word[1] or word[3] or  word[2]:
      print("Wrong! You guessed incorrectly!")
      time.sleep(1)
      lives = lives - 1
      print("You have", lives, "lives remaining!")
      time.sleep(2)


  #this is for if you lose or win

  if win == 3:
    print("You've won!")
    time.sleep(0.5)
    print("Want to play again?")
    ans = input("Yes or no: ")
  
  
  elif lives == 0:
    print("You lose!")
    time.sleep(0.5)
    print("Want to play again?")
    ans = input("Yes or no: ")


if ans == 'no':
  sys.exit
 
FOR SOME REASON THE INVALID SYNTAX IS COMING UP, I THINK THERE ARE OTHER SECTIONS OF THIS CODE INCORRECT, HOWEVER I JUST SIMPLY DON'T UNDERSTAND WHAT THE ERROR MESSAGE MEANS (INVALID  SYNTAX -  STRING INDEX OUT OF RANGE).

THANKS AND YES I AM NEW TO THE FORUM,

DARKREAPER1959
Reply
#2
1. Read the forum rules
2. Post the full traceback
and
3. Don't use ALL CAPS, that is perceived as shouting
Reply
#3
I have read the rules, sorry the caps was to make my text  stand out, I did not realise  that the code  and text is separated, my bad, I know now. I have supplied the code and given the error, here I'll copy and paste the full error (it was on python 3):

Traceback (most recent call last):
 File "python", line 32, in <module>
IndexError: string index out of range
Reply
#4
indexes are zero-based. so in this case there is no index 3.
Also your statement
if user == word[1] or word[3] or word[2]:
even after correction for the index will always evaluate to TRUE

http://stackoverflow.com/documentation/p...1370259599
Reply
#5
This is your error line
if user == word[1] or word[3] or word[2]:
should be:
if user == word[1] or user == word[3] or user == word[2]:

Plus index out of range
Reply
#6
thanks guys, really helped..
Reply
#7
What's even happening in this thread?  All of the quotes/code snippets/error messages are completely unrelated to the original post...

*edit* nevermind.  There was so much whitespace, I thought the OP only posted 6 lines of code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax Slome 2 1,970 May-13-2022, 08:31 PM
Last Post: Slome
  How to fix list index out of range longmen 26 5,924 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,340 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  List index out of range when turning CSV into dict ranbarr 15 6,439 May-12-2021, 10:38 AM
Last Post: ranbarr
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,355 Nov-07-2020, 10:17 AM
Last Post: snippsat
  Invalid syntax error, where? tucktuck9 2 3,424 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Use range to add char. to empty string johneven 5 13,848 Mar-24-2020, 11:52 AM
Last Post: mbharatkumar
  Invalid syntax defining a dictionary? ep595 6 5,109 Nov-19-2019, 08:06 PM
Last Post: ThomasL
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,081 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  list index out of range mcgrim 2 2,910 May-25-2019, 07:44 PM
Last Post: mcgrim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020