-2

Please tell me why am I getting a Sequence index is not an int, slice, or instance with __index__pylint(invalid-sequence-index) warning, I have provided the file code below.

import random
from ppwiinputs import *

print("This is PPWI\n")
end_chat = False
while end_chat == False:   
    USER_INPUT = input(">")

    if USER_INPUT.lower()  in GREETINGS:
        print(f"{GREETINGS_RESPONSE[random.randint(0,len(GREETINGS_RESPONSE)-1)]}\n")
    elif USER_INPUT.lower() in END:
        print(END_RESPONSE[random.randint(0,len(END_RESPONSE)-1)])
        quit()
    elif not(USER_INPUT.lower() in GREETINGS) or not(USER_INPUT.lower() in END):
        print("Can't find an answer to that, still learning.\n")


def grammarCorrection(USER_INPUT):
        for n in RESPONSE[range(0,len(RESPONSE)-1)]:
            for i in n:
                if (USER_INPUT in i) and (USER_INPUT != i):
                    print(f'do you mean {i}')

I have not tested the function yet, nor have I called it anywhere in the code. my VSCode started showing me this warning, and was curious to know what is it all about.
ppwiinputs.py file

GREETINGS_RESPONSE = ['HEY.', 'HOWDY.', 'WASSUP.', 'HI THERE.', "HEY, WHAT'S UP?.", 'NICE TO SEE YOU.', 'GREETINGS AND SALUTATIONS.', 'GREETINGS.']
GREETINGS = ['hi ppwi','hello ppwi','hi', 'hello']
END = ['bye', 'ok, bye', 'bye bye', 'okay bye', 'see you']
END_RESPONSE = ['BYE BYE.', 'NICE MEETING YOU, BYE.', 'BYE.', 'SEE YOU SOON.']
RESPONSE = [GREETINGS_RESPONSE, GREETINGS, END, END_RESPONSE]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Please paste the complete error in the question, that would contain the details on which like its throwing an error/warning. – Akshay Sehgal Jul 09 '20 at 12:41
  • Are you trying to skip `END_RESPONSE` from `RESPONSE` when iterating? If so, `range` isn't how you do this, you want [to slice](https://stackoverflow.com/questions/509211/understanding-slice-notation), e.g. `RESPONSE[:-1]` instead of `RESPONSE[range(0,len(RESPONSE)-1)]`. – ShadowRanger Jul 09 '20 at 12:50
  • Just an added suggestion, `random.choice(GREETINGS_RESPONSE)` does the same thing as `GREETINGS_RESPONSE[random.randint(0,len(GREETINGS_RESPONSE)-1)]` but in a much nicer way. – Axe319 Jul 09 '20 at 13:11

3 Answers3

0

This line for n in RESPONSE[range(0,len(RESPONSE)-1)]: gives the warning.

range is a class and returns an object that raise a warning because it is not recommended to use it as an index for a list.

To do what you want, you can simply slice the list with :

for n in RESPONSE[:-1]:
Jona
  • 1,218
  • 1
  • 10
  • 20
0

Not sure what list you are after, but looks like you just want to iterate over list (of lists) RESPONSE using this line for n in RESPONSE[range(0,len(RESPONSE)-1)]: . You can simply replace it with for n in RESPONSE: if that is the case.

Vijay
  • 778
  • 4
  • 9
0

First off, just to clarify. It's not a warning, it's an error. I didn't run the code calling the function, but if someone does is gonna get a TypeError. As the others answers say, it's happening because you're trying to use a range object to get values from a list and you can't do that because a range object creates the values of the iteration on the fly so it needs something else traverses it e.g. with a for loop to print the values unlike using slices in a list.

Besides, if you're using pylint as linter in VSCode I suggest you check pyling messages every time you wanna know why you're getting some warning or error and you'll understand much better what's going on. For instance, search for the error invalid-sequence-index says "Used when a sequence type is indexed with an invalid type. Valid types are ints, slices, and objects with an index method."

Hope this help you supporting the other answers :)

Brad Figueroa
  • 869
  • 6
  • 15