0
What am I doing wrong?
print("Are you familiar with artificial intelligence and bots? Please pick from the following list:\n ") fam_w_AIbts_list = ("I can make them", "Decently familiar", " Somewhat familiar", "Not sure", "Not too familiar", "Just barely familiar", "Not at all familiar") print(fam_w_AIbts_list) user_fam_w_AIbts = input() if user_fam_w_AIbts in fam_w_AIbts_list[0, 2]: print("Oh nice!") The above code gives me an error of "tuple indices must be integers or slices, not tuple." I am new :|
14 Antworten
+ 3
maybe you meant to do list slicing, so the separator of indices in bracket must be the colon, not the comma:
[0:2] instead of [0,2]
+ 2
read my last post ;P
slicing return a sublist of elements...
you could also use a range object, to make a list (generator -- could be iterated only once) of indices to test if user input is inside (user input must be also converted to number):
if int(user_input) in range(0,3)
or range(1,4) if indices start at 1...
you can store a "real" list in a variable to use it more than once:
indices = list(range(0,3))
+ 1
Dea Mer
my question was: what do you expect by doing list[0,2] ?
... as inside brackets of a list, the comma is interpreted as tuple items separator, so you're doing list[(0,2)] wich is illegal as indices must be either integer (alone) or slice (2 or 3 integers colon separated)...
+ 1
list slicing work as: list[start:stop:step]
start is the starting index (included)
end is the ending index (not included)
step is the step to skip indexes (optional)
+ 1
the user input should match exactly the string in the sliced list (case sensitive -- you could lower or upper case all string to match case unsensitive)
to test if user input is one of the 3 first elements, you must list[0,3]...
+ 1
if you input user for the index of the element, you must decide if it is supposed to start at 0 or at 1...
then you only have to test if 0 <= int(user_input) <= 2 (or 1 <= int(user_input) <= 3, depending on your initial choice), as inputs are string if you doesn't convert them to another type ^^
0
No more error but it's skipping "Oh nice!" now
0
what's the purpose of your code?
I simply guessing that you want to check if user input was inside the first two elements of the list ^^
0
Oh that's just a sample; I'm trying to make a chatbot thing
0
Ohhh, I see. I'm trying to get "Oh nice!" to print if the user's input matches indices 0, 1, and 2 of fam_w_AIbts_list. I no longer get the error, but Pydroid is skipping the last print statement even when the input is correct.
0
Yeah, I fixed that. Do you know how to get "Oh that's nice!" to print
0
Input matches string. I did
famAIbts_list = ["I can make them", "Decently familiar", " Somewhat familiar", "Not sure", "Not too familiar", "Just barely familiar", "Not at all familiar"]
print(famAIbts_list[0,3])
and got the "indices must be string or integer, not tuple" error again. However, when I do
print(famAIbts_list[0:3])
it displays the values
0
Do I need a for loop?
0
Oh didn't see, my bad. I think I got it, thank you so much!!! Very much appreciated 😁