可选功能为用户选择 [英] optional function to choose for user

查看:164
本文介绍了可选功能为用户选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用许多函数编写了一个程序( def name():)。
这些函数在代码的最后加起来就像:

$ c $ a()+ b()+ c()+ d( )+ e()
我可以这样做:

program: >>> a,b,c,d,e是可选功能,您想在计算中使用这些功能中的哪一个?

user: >>> a,b,d



,程序只是将这些选定的函数放在程序中。



我做了很多搜索,但是我找不到像这样的东西。
感谢您的帮助。

解决方案

您可以通过以下方式使用字典。

  def a():
return 2 + 3

def b():
返回3 - 2

def c():
返回2 * 3

def d():
返回2/3

dic = {}
dic ['a'] = a
dic ['b'] = b
dic ['c'] = c
dic ['d'] = d

funcs = str(raw_input(你想用哪个函数?:))
funcs = funcs.split(',' )

结果= 0

for funcs:
result + = dic [i]()

打印结果


I wrote a program using many functions (def name():). These function are summed at the end of code like:

a()+b()+c()+d()+e() In what way I can do this:

program: >>>a,b,c,d,e are optional fuctions, which one of these functions you want to use in the calculation?

user: >>>a,b,d

and the program just bring those chosen functions in program.

I did search a lot but I could not find sth like this. Thank you for your help.

解决方案

You could use a dictionary in the following way.

def a():
    return 2 + 3

def b():
    return 3 - 2

def c():
    return 2*3

def d():
    return 2/3

dic = {}
dic['a'] = a
dic['b'] = b
dic['c'] = c
dic['d'] = d

funcs = str(raw_input("which functions would you like to use?: "))
funcs = funcs.split(',')

result = 0

for i in funcs:
    result += dic[i]()

print result

这篇关于可选功能为用户选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆