+ 4
Can anyone tell me how this code works.
It prints the words that contain the letter that's been input....but how?i dont know how this worked pls tell me. https://code.sololearn.com/cm75gh58Q8z5/?ref=app
15 ответов
+ 3
It checks whether the provided stirng (command) of input is existed in given list (words) or not.
Returns the same stirng if it exist.no output otherwise.
+ 2
words = ["cat", "car", "code", "home", "learn",
"fun", "job", "love", "friend", "zoo", "enjoy",
"happiness", "family", "goal", "desire"]
#your code goes here
command = input()
if command in words:
print(command )
else:
print(command ,": not found in words ")
Here made it better....
+ 1
Thanks...but im still not clear though
+ 1
You took had a variable containing an array..
After that you took an input which is command
You looped through the array by saying telling python interpreter that
"Hey,look at these stuffs in my array ,"
You then gave the interpreter another condition that
"If this input Is in my array, please print out this input."
"But if it's not in my array,print nothing!"
That's what happened in that code.
Hope this helped?
+ 1
I don't know anything about that coding:D
+ 1
First we have a list called words and then it takes an input
command = input()
for i in words: i is the item in the words
if command in i:
print(i)
It will iterate through the list this means the items in the words will be in the "i" so if python find the command in the "i" it will print it
+ 1
if i in words, i mean.
0
Ok
0
Oh ok i understand now thank you
0
You are getting the input from user. Then you are iterating the list and checking if the input is present. If it is, then you printing the input.
0
words = ["cat", "car", "code", "home", "learn",
"fun", "job", "love", "friend", "zoo", "enjoy",
"happiness", "family", "goal", "desire"]
#your code goes here
for i in words:
if in words:
print(i)
This is your right code.
The i itters the list til the end. While the print(i) prints the words out.
0
Ok thanks for the explanation
0
It should be <<if command == i>> and not <<if command in i>> because if someone input letter "a" it will output all "a" characters in every word containing the letter "a"
Just run your current code and input "a" to understand the nuance.