Python 如何使用户调用函数
假设我有一个叫做intercept,Bobby,Jimmy和Tom的函数。我希望用户键入runintercept 这些名称是配置文件 我是python新手,所以我处理这件事的方式只是Python 如何使用户调用函数,python,Python,假设我有一个叫做intercept,Bobby,Jimmy和Tom的函数。我希望用户键入runintercept 这些名称是配置文件 我是python新手,所以我处理这件事的方式只是 input() 问题是,如果用户类型运行intercept,它将不会运行intercept函数,因为用户输入是字符串 我也不能使用if语句 userInput = input() if userInput == "intercept": intercept() 因为如果用户编写run jimmy,我需要
input()
问题是,如果用户类型运行intercept,它将不会运行intercept函数,因为用户输入是字符串
我也不能使用if语句
userInput = input()
if userInput == "intercept":
intercept()
因为如果用户编写run jimmy,我需要它运行。我可以用elif语句
userInput == input()
if userInput = "intercept":
intercept()
elif userInput = "jimmy":
jimmy()
else
create_new_profile()
但关键是,如果没有任何名称下的配置文件,他们将创建一个新的配置文件并自动添加另一个函数。这是行不通的,因为这样每次我添加一个新的概要文件时,我都必须手动添加一个elif语句
我需要一个程序采取的方式,了解输入是寻找一个函数,搜索函数列表,并运行正确的函数
Idk由于我缺乏经验,这听起来是多么原始或愚蠢,但如果您能帮助我,我将不胜感激我强烈建议不要这样做。。。但是你可以用eval。这是在使用python2.7,在使用python3.x时,您应该能够使用输入而不是原始输入: 结果是:
python run_options.py
banana
Oh, a banana.
python run_options.py
apple
Mmm apples.
直接调用未知用户输入是相当危险的。 允许的函数名可以作为键放在指向实际函数的字典中:
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
输入函数名称:intercept
函数拦截。
$python3 test.py
输入函数名称:foobar
错误:未知函数。
也可以动态创建功能,例如:
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
输入函数名称:intercept
函数拦截。
输入函数名称:Jimmy
为Jimmy创建个人资料
输入函数名称:Alice
为Alice创建配置文件
输入函数名称:Alice
爱丽丝简介
输入函数名:Bob
为Bob创建配置文件
输入函数名:Bob
纵断面摆锤
输入函数名称: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
输入函数名称:intercept
函数拦截。
输入函数名称:Jimmy
用户:吉米
输入函数名称:Alice
为Alice创建新的配置文件
输入函数名称:Alice
用户:爱丽丝
谢谢,我刚刚测试过它,它可以工作了。如果有多个配置文件Jimmy,Tommy…,如果他们调用的函数名不存在,是否可以添加另一个函数。就像它告诉他们给一个名字一样,用户给一个名字,然后它用这个名字创建一个函数,这个函数可能是
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()