【问题标题】:How can i make my python3 code do many "if input" operations at once如何让我的 python3 代码一次执行许多“if input”操作
【发布时间】:2015-03-04 16:58:29
【问题描述】:

我试着做一个人工智能只是为了看看我能不能,我不能。但我希望它至少可以工作。 这是我的代码:

while True:
   if input(":") == "hello":
       print("Hello.")
   if input(":") == "good bye":
       print("Bye!")
   if input(":") == "how are you":
       print("Good, i don't feel much. You know, I'm an AI.")

如果你运行它,你会发现它不是 AI 会做的。

【问题讨论】:

  • 您现在每次都要求输入。相反,尝试执行一次input(":") 并将结果存储在一个变量中,然后针对该变量写入您的ifs。另外,if True: 的意义何在?
  • 对不起,我的代码中有错字,我实际上是真的

标签: if-statement python-3.x input artificial-intelligence


【解决方案1】:

您可以使用while 循环,这样只要用户不输入“再见”,您的代码就会继续执行,就像这样。我还输入了else 语句,以防用户输入您的 AI 无法处理的内容。

user_input = ""
while user_input != "good bye":
   user_input = input(":")
   if user_input == "hello":
       print("Hello.")
   elif user_input == "how are you":
       print("Good, i don't feel much. You know, I'm an AI.")
   else :
      print("Say something I understand")
print("Bye!")

如果这不是您想要的,或者您有什么不明白的地方,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多