2017-04-23 26 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.