2017-04-23 24 views
0

假設我有一個叫做攔截,Bobby,Jimmy和Tom的函數。我想用戶鍵入「運行攔截」如何使用戶呼叫某個功能

名稱是型材

我是新來的蟒蛇,所以我處理這個方法就是

input() 

的問題是,如果用戶類型「運行攔截」它不會運行攔截功能導致用戶輸入的是字符串。

if語句

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

因爲如果用戶寫道:「吉米跑」我需要它來運行我還不能使用。我可以用一個elif的語句

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

但問題是,如果沒有一個配置文件以任何名義,他們將創建一個新的配置文件,並自動添加其他功能。這是行不通的,因爲我不得不手動添加一個elif語句,每次我添加一個新的配置文件

我需要一種方法讓程序瞭解輸入是尋找一個函數,搜索函數列表,並運行正確的功能。

IDK多麼原始或白癡這聽起來是由於我缺乏經驗,但我會很感激,如果你有人可以幫助我

+0

[調用模塊從Python中的函數名稱的字符串的函數(http://stackoverflow.com/questions/3061/calling-a-function-of-的可能的複製一個模塊從一個字符串與函數名稱在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以添加新用戶。以下示例將用戶數據實現爲包含正在運行的函數的類。

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你應該能夠使用input而非raw_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.