如何从def函数调用操作-Python

如何从def函数调用操作-Python,python,Python,我有如下python代码 def call(): input1 = input('Bot1:') input2 = input('Bot2:') call() input1 如何仅调用“input1”操作。我想在调用它之后,input1操作将开始在屏幕上输入数据 但是在上面的代码中。。。当我运行时,它显示警告“input1未定义” 谢谢你 不能从函数外部访问函数的局部变量。解决该限制的一种方法是这样做: ACTION1, ACTION2 = 1, 2 def get_input(act

我有如下python代码

def call():
 input1 = input('Bot1:')
 input2 = input('Bot2:')
call()

input1
如何仅调用“input1”操作。我想在调用它之后,input1操作将开始在屏幕上输入数据

但是在上面的代码中。。。当我运行时,它显示警告“input1未定义”


谢谢你

不能从函数外部访问函数的局部变量。解决该限制的一种方法是这样做:

ACTION1, ACTION2 = 1, 2

def get_input(action):
    if action == ACTION1:
        return input('Bot1:')
    elif action == ACTION2:
        return input('Bot2:')
    else:
        raise RuntimeError('Unknown action')

input1 = get_input(ACTION1)

有了这段代码,您就不能从外部“调用input1”
input
。请将您的
input()
函数重命名为其他函数,并用新名称调用它。@martineau怎么办?你能给我举个例子吗?将
def input():
更改为
def something\u else():
@martineau不正确。我已更改并调用“input1”,但无法。