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.

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
14 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
skiddy
1 year ago

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.

Lucon
1 year ago

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.

skiddy
1 year ago
Reply to  Lucon

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.

Swe4rX
1 year ago

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

skiddy
1 year ago
Reply to  Swe4rX

Elements of a list that are, for example, “str” type can be compared with a string. For example, the following can work:

x = „abc“
y = [„abc“, „def“]
if x == y[0]:
  print(True)
else:
  print(False)

In this case, “True” would be issued.

Swe4rX
1 year ago
Reply to  skiddy

Of course you can compare with elements from a list, but not with a list itself I have written so rest

whgoffline
1 year ago

Just write:

if Eingabe in Frage:
  print('Gefunden')
Swe4rX
1 year ago
Reply to  whgoffline

How to format it with syntax highlighting?

I don’t know a good question

skiddy
1 year ago
Reply to  Swe4rX

Simply use the CMD tool or code tool. This automatically recognizes common languages.

whgoffline
1 year ago
Reply to  Swe4rX

About the source button: comment image

whgoffline
1 year ago

Haha, okay was just confused about CMD.

skiddy
1 year ago

I was referring to that too.

Swe4rX
1 year ago

I think more

whgoffline
1 year ago

I think meant the source code tool comment image