簡體   English   中英

如何從 def 函數調用操作 - Python

[英]How to call an action from def function - Python

我有如下的python代碼

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

input1

如何僅調用“input1”操作。 我想調用它后, input1 動作將開始在屏幕上輸入數據。

但是在上面的代碼中......當我運行時,它顯示警告'input1 not defined'

謝謝!

您無法從函數外部訪問函數的局部變量。 解決該限制的一種方法是執行以下操作:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM