2017-04-23 17 views
0

ここでは、intercept、Bobby、Jimmy、Tomという関数があります。私は、ユーザーが関数を呼び出すにはどうすればいいですか?

「を実行インターセプト」と入力する名前は、プロファイル

ある場合、私はこれに近づいてる方法は問題がちょうど

input() 

ですので、私は、Pythonに新たなんですユーザータイプは「インターセプトを実行」します。ユーザー入力が文字列であるため、インターセプト関数が実行されません。

userInput = input() 
if userInput == "intercept": 
    intercept() 

場合、私はまた、ユーザが、私はそれを実行するために必要な「実行ジミー」を書き込むためならば使用することはできません。私はelifの声明

userInput == input() 
if userInput = "intercept": 
    intercept() 
elif userInput = "jimmy": 
    jimmy() 
else 
    create_new_profile() 

を使用することができますが、ポイントは、任意の名前の下にプロファイルがない場合は、新しいプロファイルを作成して、自動的に別の機能を追加することです。これは、私は新しいプロファイルを追加するたびにelif文を手動で追加する必要があるため、これはうまくいきません。

私はプログラムが入力を理解するためには関数を探しており、正しい機能を実行してください。

これが原因経験の私の不足に聞こえるが、私は本当に感謝されるか、プリミティブまたは馬鹿IDK誰かが私に

+0

可能な重複a-module-from-the-function-name-in-python) –

答えて

1

直接、未知のユーザの入力を呼び出すことは非常に危険です。 許さ関数名は、実際の機能を指している辞書にキーとして置くことができます。

def intercept(): 
    print("Function intercept.") 

user_functions = dict(
    intercept=intercept, 
    # ... 
) 

user_input = input("Input function name: ") # Python 3, raw_input for Python 2 

if user_input in user_functions: 
    user_functions[user_input]() 
else: 
    print("Error: Unknown function.") 

結果:

$ python3 test.py 
Input function name: intercept 
Function intercept. 

$ python3 test.py 
Input function name: foobar 
Error: Unknown function. 

機能も動的に作成することができ、例えば:

def intercept(): 
    print("Function intercept.") 

def jimmy(): 
    print("Function Jimmy.") 

user_functions = { 
    'intercept': intercept, 
    # ... 
} 

def create_new_profile(name): 
    print("Creating profile for " + name) 
    def profile_function(): 
     print("Profile " + name) 

    user_functions[name] = profile_function 


while 1: 
    user_input = input("Input function name: ") 

    if user_input in user_functions: 
     user_functions[user_input]() 
    else: 
     create_new_profile(user_input) 

    print() 

結果:

$ python3 test.py 
Input function name: intercept 
Function intercept. 

Input function name: Jimmy 
Creating profile for Jimmy 

Input function name: Alice 
Creating profile for Alice 

Input function name: Alice 
Profile Alice 

Input function name: Bob 
Creating profile for Bob 

Input function name: Bob 
Profile Bob 

Input function name: Alice 
Profile Alice 

私は「プロファイル機能」が何をすべきかわかりません。たぶん、データ駆動型アプローチが良いかもしれません。辞書は、ユーザー名をユーザーデータにマップします。ユーザー入力名がプロファイル辞書内にない場合は、新しいユーザーを追加する関数create_new_profileが呼び出されます。次の例では、実行されている関数を含むクラスとしてユーザーデータを実装しています。(http://stackoverflow.com/questions/3061/calling-a-function-of- [Pythonで関数名を持つ文字列からモジュールの関数を呼び出す]の

class UserProfile: 
    def __init__(self, name): 
     self.name = name 

    def run(self): 
     print("User: " + self.name) 


def intercept(): 
    print("Function intercept.") 

user_profiles = { 
    'Jimmy' : UserProfile('Jimmy'), 
} 

def create_new_profile(name): 
    print("Creating new profile for " + name) 
    user_profiles[name] = UserProfile(name) 

user_functions = { 
    'intercept': intercept, 
    # ... 
} 

while 1: 
    user_input = input("Input function name: ") 

    if user_input in user_functions: 
     user_functions[user_input]() 
    elif user_input in user_profiles: 
     user_profiles[user_input].run() 
    else: 
     create_new_profile(user_input) 

    print() 
$ python3 test.py 
Input function name: intercept 
Function intercept. 

Input function name: Jimmy 
User: Jimmy 

Input function name: Alice 
Creating new profile for Alice 

Input function name: Alice 
User: Alice 
+0

ありがとう、私はちょうどそれをテストし、それが動作します。複数のプロファイル(Jimmy、Tommy ...)がある場合、呼び出された関数名が存在しない場合は、別の関数を追加する方法はありますか?名前を付けるように指示するのと同様に、ユーザーは名前を付け、その名前で関数を作成します – intercept

0

を助けることができるあなたならば、私はhightlyこれに対して助言...しかし、あなたはevalを使用することができます。これは、python2.7を使用しているpython3.xとするのではなくraw_inputよりもinputを使用することができるはずです。

def apple(): 
    print('Mmm apples.') 


def banana(): 
    print('Oh, a banana.') 


user_input = raw_input() 
eval(user_input + '()') 

と結果:

python run_options.py 
banana 
Oh, a banana. 

python run_options.py 
apple 
Mmm apples. 
関連する問題