How to recognize name (from text file) in user input and then print name pt.2

江枫思渺然 提交于 2020-01-16 19:38:12

问题


I was asked to provide the problem (wrong output), that I got while running my code to How to recognize name (from text file) in user input and then print name, so here is the problem/extra explanation on what I'm trying to archive with this program.

Let's say the text file (my database) is completely empty with no information provided by the user until now (like you never mentioned having a brother/sister in the beginning). So, when you input the keywords a brother/brother (I haven't had time to implement sister). The chat bot checks both the database (which is again empty at the moment) and their dictionary:

brother_status = dict([
    ('name', ''),
    ('nickname', ''),
    ('current age', ''),
    ('relationship', '')])

and returns this print/raw_input to enter their name:

what type of sibling do you have: brother
You never mentioned a brother. What's his name?
What's his name: James

The chat bot then adds that name into the brother_status[name] / text file, but after it tries to continue the conversation by asking you to repeat what you said about your brother (James). The print ("Oh, so your brother's name is" + line.split(':')[1] * 1) (from the code above) prints too, since the text file isn't empty anymore and the name James was mentioned by the user.

I'll make sure to remember that, so what about James?
Oh, so your brother's name is James

But I want ("Oh, so your brother's name is" + line.split(':')[1] * 1) to only print if the user mentioned that name first/if the text file isn't empty and you already provided information about your brother/sister.

what type of sibling do you have: James
Oh, so your brother's name is James 

So I decided to make some adjustments by separating the code from the main program and making it only print if the length of the user input is greater than 1 (For example, if I said "James is annoying" or "I can't stand James" then it would print) to avoid the problem from before, but then I tested it out with other stuff:

Please enter brother's name: J is annoying
Oh, so your brother's name is James 

Please enter brother's name: I can't stand J
Oh, so your brother's name is James

Please enter brother's name: m is annoying 
Oh, so your brother's name is James 
Oh, so your brother's name is James (Yes, it repeats itself)

How can I get it to only print, when the user uses the name?

Separate code, I'm using to figure out this problem (original in my previous question):

import string

user_input = raw_input("Please enter your brother's name: ").translate(string.maketrans("",""), string.punctuation)    

with open('file.txt') as sibling_database:
if len(user_input.split()) >= 2:
    for line in sibling_database:
        for word in line.split(':'):
            for words in user_input.split():
                if words in word:
                    print("Oh, so your brother's name is " + line.split(':')[1])

(Sorry for the use of Python 2, I have yet to learn 3)

(If Python 3 is easier to use to solve my problem, I don't mind you changing it)


回答1:


The reason why you're still getting a response when using test sentences like

J is annoying

Is because of the condition:

 if words in word:

Check what it matches. For this particular case, the letter j will be found in james, so the bot finds a sort of match, and returns the name.

You could replace "in" by "==" forcing it to make a full match. But in that case you should also convert all capital letters to small letters, otherwise they wont match.



来源:https://stackoverflow.com/questions/59543438/how-to-recognize-name-from-text-file-in-user-input-and-then-print-name-pt-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!