Python Forum
But why? Because of the for loop?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
But why? Because of the for loop?
#1
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 == ""
Reply
#2
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.
Reply
#3
But shouldn't it jump into the loop down first or is it because of the wordorder?
Reply
#4
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.
Reply
#5
Nevermind I figured it out now......way more complicated but it should work. THANKS to everyone for the help.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020