[英]Can I use a function within that same function?
I am trying to make a sort-of AI thing with Python, but I've got stuck on some part.我正在尝试用 Python 制作一种人工智能的东西,但我在某些方面陷入了困境。
When the AI says something, (says hello at the beginning), it gives you the ability to input text, once you've entered that, it would go through if 'keyword' in answer statements to provide a reply.当 AI 说话时,(开头打招呼),它使你能够输入文本,一旦你输入了,它会 go 通过 if 'keyword' in answer statements 来提供回复。 Once it replies, it calls the AImain() function, restarting that process.
一旦回复,它就会调用AImain() function,重新启动该进程。 I can't seem to get it to restart, as compiling would just say hello initially (as intended), but give no input text ability, which results in the script ending.
我似乎无法让它重新启动,因为编译最初只会打招呼(如预期的那样),但没有提供输入文本功能,这导致脚本结束。 Here's the code:
这是代码:
def AImain():
response = input("YOU:")
if 'Hello' or 'Hi' in answer:
print(random.choice(welcomeMessages))
AImain()
I'm new to python and I don't understand why this isn't working.我是 python 的新手,我不明白为什么这不起作用。 Thanks!
谢谢!
Your code has an indentation problem at the Aimain()
function and we can't see how answer
is declared.您的代码在
Aimain()
function 处存在缩进问题,我们看不到answer
是如何声明的。 You are assigning the input to response
but reading answer
in your code.您正在将输入分配给
response
,但在代码中读取answer
。
Here is my suggestion to your code这是我对您的代码的建议
response = "Hi" #you can eliminate this, assuming response is declared somewhere else def AImain(): global response response = input("You:") while response == 'Hello' or response == "Hi": print(random.choice(welcomeMessages)) AImain()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.