简体   繁体   中英

Why am I getting this error? This error wasn't expected

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]

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]:

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.

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 eg 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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM