【问题标题】:'While True' loop does not run“当真”循环不运行
【发布时间】:2025-12-07 22:25:02
【问题描述】:

我的代码:

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()

当我输入“hello”时,程序给出了预期的响应,但它只是退出而没有完成循环。哪条线打破了循环? 谢谢。

【问题讨论】:

  • 它适用于我的机器:D
  • 我无法重现这个。在 repl.it 上执行时,它会循环。
  • 我在 PyCharm 上运行
  • 尝试在终端中运行代码,我最近遇到了类似的问题,当我更换终端时它工作了。
  • 你可能不想调用变量 self ..

标签: python python-3.x loops while-loop


【解决方案1】:

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

也许您正在使用 python 2.x 进行编译。在这种情况下,您需要使用 'raw_input' 而不是 'input',但我不推荐 raw_input(它在 Python3 中已弃用)。

试试 Python 3.x。

PS:另外,我会将“self”替换为其他变量名。自我用于类。

【讨论】:

  • 我使用的是python 3.8 32位
  • 在 python 3.8 64 位 GNU/Linux (Trisquel) 中对我来说很好。确保您的编译版本。祝你好运!
【解决方案2】:

它不会损坏。请验证您的解决方案。这是修改后的代码

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()

【讨论】:

    【解决方案3】:

    我完全无法重现。就像代码所说的那样,它不断提示输入新的输入。

    顺便说一句,这里稍微简化一下,使用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()
    

    【讨论】:

      【解决方案4】:

      此代码在我的机器上完美运行。但是 Python 2.7 版本的问题。我更喜欢你使用 Python 3.6 或更高版本。

      这是一个简化的代码。

      代码:

      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() 
      

      检查一下。 我希望它会有所帮助。

      【讨论】: