如何在用户输入中识别名称(来自文本文件),然后打印名称 pt.2 [英] How to recognize name (from text file) in user input and then print name pt.2

查看:27
本文介绍了如何在用户输入中识别名称(来自文本文件),然后打印名称 pt.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求提供问题(错误的输出),这是我在将代码运行到 如何识别名称(来自文本文件),然后打印名称,所以这里是我试图用这个程序存档的问题/额外解释.

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', '')])

并返回此 print/raw_input 以输入其名称:

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

聊天机器人然后将该名称添加到 Brother_status[name]/文本文件中,但之后它会尝试通过要求您重复您对兄弟(詹姆斯)所说的话来继续对话.打印(哦,所以你兄弟的名字是"+ line.split(':')[1] * 1)(来自上面的代码)也会打印,因为文本文件不再是空的,而 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

但是我想要 ("Oh, so your Brother's name is" + line.split(':')[1] * 1) 仅在用户首先提到该名称/文本文件不为空时才打印并且您已经提供了有关您兄弟/姐妹的信息.

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 

因此我决定通过将代码与主程序分离并使其仅在用户输入的长度大于 1 时才打印(例如,如果我说James 很烦人"或我可以't stand 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])

(抱歉使用Python 2,我还没学3)

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

(如果 Python 3 更容易解决我的问题,我不介意你改变它)

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

推荐答案

使用像

J很烦

是因为条件:

 if words in word:

检查匹配的内容.对于这种特殊情况,字母 j 将在 james 中找到,因此机器人会找到一种匹配项,并返回名称.

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.

您可以将in"替换为=="以强制其进行完全匹配.但在这种情况下,您也应该将所有大写字母转换为小写字母,否则它们将不匹配.

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.

这篇关于如何在用户输入中识别名称(来自文本文件),然后打印名称 pt.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆