Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare input() to textfile
#3
I'm very confused by your explanation. The phrase "I like to eat apples" contains only two words that appear in the file you mentioned, yet you want to output a message saying 'I do not understand orange, bake, and banana'. How are you deciding what to output?

Some basic commands to learn:
  • To open a file and read contents:
    with open('myfile.txt') as filename:
        words = filename.read()
    The words variable now references a string holding the contents of the file. A '\n' character will mark each line in the string.
  • To get the user input: phrase = input('Enter a phrase: ')
  • To break up a phrase into distinct words, use the split() string method.
  • To loop through words in a list of words, use a for loop, e.g. for word in phrase_words:

You need to be clear on the problem you are trying to solve, and how to solve it (as a set of tasks, rather than specifically as a python programme at first). Think how you would do it manually if breaking it down to very basic steps rather than making human leaps.
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,881 Aug-31-2023, 02:50 AM
Last Post: deanhystad
  Importing python data to Textfile or CSV yanDvator 0 1,792 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,403 Jan-14-2020, 11:43 AM
Last Post: Formationgrowthhacking
  Replace Line in Textfile Deadline 1 10,419 Nov-04-2019, 07:14 PM
Last Post: Larz60+
  Compare all words in input() to all words in file Trianne 1 2,797 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Write from URL with changing information to textfile wmc326 1 3,033 Jul-12-2017, 07:10 PM
Last Post: wavic
  Loop thru textfile, change 3 variables for every line herbertioz 11 9,217 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