简体   繁体   English

我可以在同一个 function 中使用 function 吗?

[英]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.

相关问题 我可以为变量分配函数以便在另一个函数中使用吗? - Can I assign a function to a variable for use within another function? 如何在同一个 function 中使用 function 的值 - How to use the value of a function within the same function 如何使用 function 的结果作为相同 function 的参数? - How can I use the result of the function as parameter for the same function? 为什么我不能在熊猫查询功能中使用包含? - Why can't i use contains within query function of pandas? 如何在keras模型中使用tensorflow度量函数? - How can I use tensorflow metric function within keras models? 类:函数在同一个类中使用 - Classes: Function Use within the same Class 如何使用相同的回调函数来跟踪多个变量? - How can I use the same callback function to trace multiple variables? 我可以使用字典来调用相同的函数但不同的参数吗? - Can I use dictionary for calling same function but different parameters? 在Python中,如何在第二个函数中使用函数的return语句而不使用全局变量? - In Python, how can I use a function's return statement within a second function without using global variables? 不能在函数 Python 中使用 random - Can't use random within a function Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM