neolee / pilot

进入编程世界的第一课
584 stars 840 forks source link

第一次课程大练习程序优化 #1516

Open pyz610173878 opened 1 year ago

pyz610173878 commented 1 year ago

再正则表达式Eliza 模块中,发现只要输入quit,程序就可以终止。于是想把此功能加入到对话机器人中,但尝试一番无法加入。以下是思考过程。

  1. 先理解了Eliza 模块中是如何做到的。使用循环只要输入不等于quit,就把值传入respnd方法,匹配正则表达式,并随机替换相应的字符串。
def command_interface():
    print('Therapist\n---------')
    print(
        'Talk to the program by typing in plain English, using normal upper-')
    print('and lower-case letters and punctuation.  Enter "quit" when done.')
    print('=' * 72)
    print('Hello.  How are you feeling today?')
    s = ''
    therapist = Eliza()
    while s != 'quit':
        try:
            s = input('> ')
        except EOFError:
            s = 'quit'
        while s[-1] in '!.':
            s = s[:-1]
        print(therapist.respond(s))

if __name__ == "__main__":
    command_interface()
  1. 一开始想当然的以为可以直接照搬此方式,但验证了很多次都无法正常运行,然后尝试简化需求,做到停止一个子类Bot。但我想一个能停止,肯定都能停止,但发现两者之间的逻辑貌似不相同。
  2. 进一步在在Garfield类中添加判断条件,来终止程序。尝试一番还是不行。

老师能否给点思路。

neolee commented 1 year ago

没明白你的难点在哪里,把你尝试过的(完整)代码发出来看看?