+ 2
Fix for error?
Every time I run my code I get this error: “Traceback (most recent call last): File “...\Playground\”, line 45 in <module> if prg[prgPos] == letters[0]: IndexError: string index out of range” Code in answers.
2 ответов
+ 2
Try incrementing the prgPos variable after the equality test. Also, the code will be stuck in the while loop; the RunState bool isn't ever set to False.
+ 3
Code:
#Asks the user to input the code.
prg = input()
#Position of the 'parser?'.
prgPos = 0
#Letters that can be printed.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
#Symbols that can be printed.
symbols = ["!", "?", " "]
#If the code is running or not.
#*Is only ever set to false when the code is done running or an error occurs!*
RunState = True
#Runs the code.
while RunState == True:
#Increases prgPos by 1
prgPos += 1
#To be used during debugging.
"""print(prgPos)"""
#CODE CAUSING THE ERROR \/
#This big chunk of code checks the program for letters, symbols, and commands.
if prg[prgPos] == letters[0]:
print(letters[0])