Python Forum
But why? Because of the for loop? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: But why? Because of the for loop? (/thread-7249.html)



But why? Because of the for loop? - hello_its_me - Dec-30-2017

Hi everyone,
I am working on a program that decides if the content gets googled or if it searches for images. I filter the words and remove words that are not needed but there is a problem I can't figure out how to solve. If I input "search for pictures of Bitcoin" I get the output:
I will search for: pictures Bitcoin
I will search for pictures of: Bitcoin

Is it because of the for loop? I tried to remove words if they are in google so the loop will never be used but I didn't work:

Here is my code:
google_loop = 0
pictures_loop = 0


cut_words_google = ["for",  "online", "of", "me"]
cut_words_pictures = ["for", "online", "of", "me", "google", "search", "look", "online"]
google = ["google", "search", "look", "online"]
pictures = ["show", "images", "pictures", "photos"]
words = input()
test = words
 
for word in words.split(" "):
	if word in pictures and word not in google:
		words = ""
		pictures_loop = 1

	if word in google and word not in pictures:
		words = ""
		google_loop = 1

while google_loop == 1:
	for word in google:
		test = test.replace(word, "")
	for word in cut_words_google:
		test = test.replace(word, "")
		test = test.replace("  ", " ")
	print("I will search for: " + test)
	google_loop = 0
	test == ""
	words == ""

while pictures_loop == 1:
	for word in pictures:
		test = test.replace(word, "")
	for word in cut_words_pictures:
		test = test.replace(word, "")
		test = test.replace("  ", " ")
	print("I will search for pictures of: " + test)
	pictures_loop = 0
	test == ""
	words == ""



RE: But why? Because of the for loop? - squenson - Dec-30-2017

In your case, the two if's between lines 13-19 are True because of two different words. Then both variables are equal to 1.


RE: But why? Because of the for loop? - hello_its_me - Dec-30-2017

But shouldn't it jump into the loop down first or is it because of the wordorder?


RE: But why? Because of the for loop? - squenson - Dec-30-2017

With the first word, "search", the second if is activated, then the for loop continues and when it reaches "pictures", the first if is activated as well.

If you want to stop the scanning of words after the first or second if is True, then add the break statement in line 16 and 20.


RE: But why? Because of the for loop? - hello_its_me - Dec-30-2017

Nevermind I figured it out now......way more complicated but it should work. THANKS to everyone for the help.