0
If Statement in (list)
Hello I Made a little code: list = ["Python","Java", "C++"] if "Python " in list == True: print ("Python!") But the Console didn't prints Python 😔 What I do wrong? 🤔 It only works with the print function: print("Python" in list) Output: True Ninja
3 Respostas
0
Hi there, a couple things:
1. on line 2, when you ask if "Python " is in the list, your string has a space after Python, which means it'll look for the word Python plus the space after, and since none exists, it's False.
2. On the same line 2, you ask the computer if "Python" in list == True, you don't need the "== True" part. It'll still work, but it's silly. Because the computer sees "If "Python" in list" and produces True, and then you ask "True == True". As you can see, there's no need to have that. It's a simple mistake though.
3. try not to use "list" as a variable name, because there's a function in python that has the name "list". So if you replace that with your own variable, you can no longer use that, and sometimes it might have unexpected behavior. This is called "shadowing" :)
Here's the corrected code if you're just curious:
alist = ["Python","Java", "C++"]
if "Python" in alist:
print ("Python!")
0
Okay 😅
0
lol, don't feel overwhelmed.
It's better that I point out the little things now, rather than later.
If you're confused about anything I said, you're welcome to ask/comment/criticize.