Python Forum
Compare all words in input() to all words in file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare all words in input() to all words in file
#2
First, file.read() returns a string. When you loop through a string (as in for counter in content:), you loop through the characters of the string. You want to loop through the words in content, not the characters. So you need to use the split method as you did for sentence.

Second, as the error you got shows, you want to test for string in list, not list in string. So flip the variables around the in operator.

Third, you first if clause is not needed. If the second if clause doesn't trigger, the loop goes to the next iteration, which is all your first if clause is doing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Compare all words in input() to all words in file - by ichabod801 - Oct-05-2018, 06:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 289 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  find and group similar words with re? cartonics 4 781 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,757 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Form that puts diacritics on the words in the text Melcu54 13 1,567 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
  docx insert words of previuos paragraph on next paragraph in the same position ctrldan 7 1,293 Jun-20-2023, 10:26 PM
Last Post: Pedroski55
  Pulling Specifics Words/Numbers from String bigpapa 2 793 May-01-2023, 07:22 PM
Last Post: bigpapa
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,157 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Need to match two words in a line tester_V 2 897 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Output difference from 2 lists of different sizes with words gracenz 5 1,369 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,557 Jun-27-2022, 08:45 AM
Last Post: Tmagpy

Forum Jump:

User Panel Messages

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