【发布时间】:2018-09-17 06:06:30
【问题描述】:
我有 10 到 20 个前缀名称相同的函数,我必须根据用户输入调用它们,但我不知道如何调用它们,我尝试使用下面的方法但它不起作用,谁能告诉我应该怎么做我使函数可调用。
def pattern_1(no):
print('First Pattern with ' +str(no)+ ' rows')
def pattern_2(no):
print('Second Pattern with ' +str(no)+ ' rows')
rows = eval(input('Enter number of rows: '))
pattern_no = eval(input('Enter pattern num [1-10]: '))
cust_fun_name = 'pattern_' + str(pattern_no)
print(cust_fun_name) # Here its print pattern_2 but why function is not get invoked
cust_fun_name()
当我在上面运行代码时出现以下错误
Traceback (most recent call last):
File "/home/main.py", line 22, in <module>
cust_fun_name()
TypeError: 'str' object is not callable
【问题讨论】:
标签: python python-3.x function