Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare input() to textfile
#1
Hello Everyone,

I am new to this forum and this is my first post.

I like to ask. Let say the user enters an input..say

"I like to eat apples". I would like the sentence to break

down into each word then compare each word to a file.

e.g. the file itself contains
-----------------------------
to
orange
bake
banana
apple
-----------------------------
then i like the output to show

print('I do not understand orange, bake, and banana')

Thank You.
Reply
#2
what have you tried so far, show code, and where you are having problem.
Is this a homework assignment?
Reply
#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
#4
Hello gruntfutuk,

I apologized for not stating my question clear.

I am comparing what the user input "I like to eat apples" to the content of file which has

to
orange
bake
banana
apple

I am looking for words that is not in the file that is why the output is "I do not understand orange, bake, and banana" because the words 'to' and 'apple' are found in user input.

I hope this is a little more clearer.

My code is as follow so far:
-----------------------------------
sentence = ('i like to eat apples')

print sentence.split()
-----------------------------------
the output will break the sentence in to each word but I don't know how to put on each line.

Thank you.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  textfile to customtkinter combobox janeik 6 1,655 Aug-31-2023, 02:50 AM
Last Post: deanhystad
  Importing python data to Textfile or CSV yanDvator 0 1,720 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,267 Jan-14-2020, 11:43 AM
Last Post: Formationgrowthhacking
  Replace Line in Textfile Deadline 1 10,311 Nov-04-2019, 07:14 PM
Last Post: Larz60+
  Compare all words in input() to all words in file Trianne 1 2,716 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Write from URL with changing information to textfile wmc326 1 2,955 Jul-12-2017, 07:10 PM
Last Post: wavic
  Loop thru textfile, change 3 variables for every line herbertioz 11 9,042 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