Need help with Python?
Hi,
I'm a complete beginner in Python, so this may be a very easy error to solve. Anyway, here's my very short code:
So if you enter the word "why" or "how" or "wherefore" or simply "?", the result should be "element found".
Unfortunately, this doesn’t work:
I understand the error, but I don't know how to fix it differently. Do you have any ideas?
Thank you very much.
For a list of elements, you can only refer to one item in the list. This means that your “if input = question[x,y,z]” does not work, but you can only do a “if input = question [x]” etc. As soon as you refer to multiple elements, you try to make an access like a tuple. A list is NO Tupel.
More simpler would be here:
“if to question” to write. The “in” is ultimately a kind of “for-gap” that rotates through all elements of the list and sees whether “input” exists in the list.
In addition, as a hint: your “str(input())” is not necessary because “input()” already returns a sting.
You should definitely deal with the basics (!)
The understanding of lists is essential.
The answer is well German: list indexes (i.e. 0.1,2,3 in question[0,1,2,3]) must be an integer (number) and not a tuple (counting).
You can make several terms with && (UND) or || (OR) in one if. So you could use if input == “?” || input == “Why” || Eing.
For the beginning, multiple if-states would be ok, but that should be avoided and put on the keywords present in Python such as “if x in y” or the like.
Believe you don’t understand the mistake
You can’t just compare a list with a string,
Try it.
“For element in question:
If element == Input:
Print…
“
This is now pseudocode that I wrote on the phone that is so likely not going but from the principle it should be right
Elements of a list that are, for example, “str” type can be compared with a string. For example, the following can work:
In this case, “True” would be issued.
Of course you can compare with elements from a list, but not with a list itself I have written so rest
Just write:
How to format it with syntax highlighting?
I don’t know a good question
Simply use the CMD tool or code tool. This automatically recognizes common languages.
About the source button:
Haha, okay was just confused about CMD.
I was referring to that too.
I think more
I think @Swe4rX meant the source code tool