简体   繁体   English

“当真”循环不运行

[英]'While True' loop does not run

My code:我的代码:

def make_response(self):
    recognised = False
    get_cmd = False
    database = {
        "hello": "Nice to meet you. What can I do for you?",
        "hi": "Nice to meet you. What can I do for you?",
        "hey": "Nice to meet you. What can I do for you?",
        "goodbye": "Bye. See you next time!"
    }

    self = self.lower()

    for i in database:
        if i in self:
            recognised = True
            value = database.get(i)
            print(value)


def robot():
    print('Welcome to robot.py')
    print('What can I do for you?')

    while True:
        query = input('>')
        make_response(query)


robot()

When I input "hello", the program gives the intended response but it just exits without completing the loop.当我输入“hello”时,程序给出了预期的响应,但它只是退出而没有完成循环。 Which line broke the loop??哪条线打破了循环? Thank you.谢谢你。

In Python 3.x, works fine.在 Python 3.x 中,工作正常。

Maybe you're compiling with python 2.x.也许您正在使用 python 2.x 进行编译。 In that case, you need to use 'raw_input' instead of 'input', but I don't recommend raw_input (it's deprecated in Python3).在这种情况下,您需要使用 'raw_input' 而不是 'input',但我不推荐 raw_input(它在 Python3 中已弃用)。

Try Python 3.x.尝试 Python 3.x。

PS: Also, I'd replace 'self' with other variable name. PS:另外,我会用其他变量名替换“self”。 Self is used in classes.自我用于类。

I'm totally unable to reproduce that.我完全无法重现。 It keeps prompting for new inputs, just like the code says.就像代码所说的那样,它不断提示输入新的输入。

As an aside, though, here's a slight simplification, using dict.items() :不过,顺便说一句,这里使用dict.items()稍微简化一下:

database = {
    "hello": "Nice to meet you. What can I do for you?",
    "hi": "Nice to meet you. What can I do for you?",
    "hey": "Nice to meet you. What can I do for you?",
    "goodbye": "Bye. See you next time!",
}


def make_response(query):
    query = query.lower()
    for keyword, answer in database.items():
        if keyword in query:
            print(answer)
            break


def robot():
    print("Welcome to robot.py")
    print("What can I do for you?")

    while True:
        query = input(">")
        make_response(query)


robot()

It doesnt break.它不坏。 Please verify your solution.请验证您的解决方案。 Here is modified code这是修改后的代码

database = {
        "hello": "Nice to meet you. What can I do for you?",
        "hi": "Nice to meet you. What can I do for you?",
        "hey": "Nice to meet you. What can I do for you?",
        "goodbye": "Bye. See you next time!"
    }


def make_response(self):
    self = self.lower()
    value = database.get(self, "Sorry I dont understand")
    print(value)


def robot():
    print('Welcome to robot.py')
    print('What can I do for you?')

    while True:
        query = input('>')

        if query == "goodbye":
            value = database.get(query)
            print(value)
            break
        else:
            make_response(query)

robot()

This Code works perfectly in my machine.此代码在我的机器上完美运行。 But problems with Python 2.7 version.但是 Python 2.7 版本存在问题。 I prefer you to work with Python 3.6 or above.我更喜欢你使用 Python 3.6 或更高版本。

Here is a simplified code.这是一个简化的代码。

Code:代码:

def make_response():
  database = {
        "hello": "Nice to meet you. What can I do for you",
        "hi": "Nice to meet you. What can I do for you?",
        "hey": "Nice to meet you. What can I do for you?",
        "goodbye": "Bye. See you next time!"
      }
  return database

def robot():
    print('Welcome to robot.py')
    print('What can I do for you?')

    while True:
        query = str(input('>'))
        database = make_response()
        if query in list(database.keys()):
            print(database[query])

robot() 

Check it out.一探究竟。 I hope it would be helpful.我希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM