Index reference error in python3

Asked 2 years ago, Updated 2 years ago, 40 views

This is a program that makes English words

Input is

7
box
photo
axis
dish
church
leaf
knife

That's it.

input_line=int(input())
words_spell=[list(input()) for i in range(input_line)]



for i in range (input_line):
    #s,sh,ch,o,x are es at the end
    if words_spell[i][-1]=="s" or words_spell[i][-1]=="o" or words_spell[i][-1]=="x" or words_spell[i][-2]+words_spell[i][-1]=="sh" or words_spell[-2]+words_spell[-1]=="ch"ch"
        words_spell[i].append("es")
    # put vees at the end of an English word except f and fe at the end of an English word with either f or fe
    elif words_spell[i][-1]=="f":
        words_spell[-1]
        words_spell[i].append("ves")
    elif words_spell[i][-1]+ words_spell[i][-2]=="fe":
        words_spell [-2:-1]
        words_spell[i].append("ves")

    # Add ies to the end except for y at the end of an English word whose last character is y and whose second character is neither a, i, u, e, or o
    elif words_spell[i][-1]=="y" and words_spell[i][-2]!="a" and words_spell[i][-2]!="i" and words_spell[i][-2]!="u" and words_spell[i][-2]!="e" and words_spell[-2]!="o:"
        words_spell[i][-1]
        words_spell[i].append("ies")

    # put an s at the end of an English word that does not meet any of the above conditions
    else:
        words_spell[i].append("s")

error message

Traceback (most recent call last):
  File "Main.py", line 12, in <module>
    if words_spell[i][-1]=="s" or words_spell[i][-1]=="o" or words_spell[i][-1]=="x" or words_spell[i][-2]+words_spell[i][-1]=="sh" or words_spell[-2]+words_spell[-1]=="ch"ch"
IndexError:list index out of range

I don't know the reason for the error.
By the way, I already submitted it, so it's not cheating

python python3

2022-09-30 21:45

1 Answers

It is not an answer because it is a question about the cause of the error, but Python can also use slice notation for stringed objects.Also,

words_spell[i][-1]=="s" or words_spell[i][-1]=="o" or...

and so on, but the endswith() methods and the in, not in operators make it easier to read.

#First line presents number of words
num_words=int(input())
# Take words
words = [ input() for_inrange(num_words) ]

# Pluralize
plurals = [ ]
For win words:
  # s,sh,ch,o,x are es at the end
  if w.ends with (('s', 'sh', 'ch', 'o', 'x')):
    w+='es'
  # except for the end f,fe of an English word ending in either f,fe
  # to attach a ves
  elif.endswith('f'):
    w = w[:-1] + 'ves'
  elif.endswith('fe'):
    w = w [:-2] + 'ves'
  # One letter at the end is y, and the second letter from the end is neither a, i, u, e, o
  # put ies at the end of an English word except y at the end
  elif w.endswith('y') and w[-2]not in('a', 'i', 'u', 'e', 'o'):
    w = w[:-1] + 'ies'
  # put an s at the end of an English word that does not meet any of the above conditions
  else:
    w+='s'

  plurals.append(w)

print(plurals)


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.