Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare input() to textfile
#5
The words "I", "like", "eat" and "apples" are not in the file. ("apple" != "apples")

I'm still not understanding. You want to print out every word in the file that was not included in the input?

I've told you how to read all the words from the file into a list called words.

You can do a for loop to step through every word in your sentence list, and for each word, if that word does not appear in the list of words, print it out. You can use the option end='' inside the print function (Python 3) to stop print from ending the line before you want it to, e.g. print(word, end=''). It might be easier to generate a new list of the missing words. You can print a comma separated list of strings using the join method: ', '.join(string_list) inside the print statement.

(As you are learning Python, I strongly recommend you use Python 3 rather than legacy Python. (Support for Python 2 ends on 1st January 2020.) Generally, I'd suggest that only experienced programmers responsible for maintaining old code, or those with a absolute dependency on a library that has not yet been updated and for which there are no Python 3 compatible alternatives should be using Python 2. Python 3 has many advances, fixed a lot of issues, and is more performant.)
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Compare input() to textfile - by Trianne - Sep-28-2018, 11:07 PM
RE: Compare input() to textfile - by Larz60+ - Sep-29-2018, 02:21 AM
RE: Compare input() to textfile - by gruntfutuk - Sep-29-2018, 11:57 AM
RE: Compare input() to textfile - by Trianne - Sep-29-2018, 02:19 PM
RE: Compare input() to textfile - by gruntfutuk - Sep-29-2018, 02:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  textfile to customtkinter combobox janeik 6 1,811 Aug-31-2023, 02:50 AM
Last Post: deanhystad
  Importing python data to Textfile or CSV yanDvator 0 1,766 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  How to compare in python an input value with an hashed value in mysql table? Formationgrowthhacking 4 3,357 Jan-14-2020, 11:43 AM
Last Post: Formationgrowthhacking
  Replace Line in Textfile Deadline 1 10,395 Nov-04-2019, 07:14 PM
Last Post: Larz60+
  Compare all words in input() to all words in file Trianne 1 2,781 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Write from URL with changing information to textfile wmc326 1 3,002 Jul-12-2017, 07:10 PM
Last Post: wavic
  Loop thru textfile, change 3 variables for every line herbertioz 11 9,160 Nov-10-2016, 04:56 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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