【发布时间】:2021-05-18 17:09:31
【问题描述】:
有史以来第一个问题!我已经构建了一个 GUI,它要求用户输入可能的 5 个值中的 2 个。每对值(10 个可能的对)用于运行名为 Case_n 的 10 个不同的求解函数,所有五个值(零和非零)都传递给这些函数。
我遇到的问题是将 bool() 结果剥离到没有括号等的 2 位数字,然后放入用于创建要调用的函数名称的变量中。
我已经运行了逻辑,将 TRUE 值添加到列表中,然后将列表转换为字符串,以便我可以将其剥离为仅数字,保存 2 位字符串并将其添加到 Case_n 名称中。现在,当我尝试使用该名称调用该函数时,我收到一个字符串不可调用的错误。请帮忙 。 . .
s = 5 #vars. For this example, I've pre-loaded 2 of them
a = 15
l = 0
r = 0
e_deg = 0
ve = 0
case = []
if bool(s):
case.append(1)
if bool(a):
case.append(2)
if bool(l):
case.append(3)
if bool(r):
case.append(4)
if bool(e_deg):
case.append(5)
nm = str(case) # placeholder to convert case to string
case_num = nm[1] + nm[4] # this returns 12 as a string
# create case_num var, using the string
Case = "Case_" + case_num
print("Case = ",Case) # Should be Case_12
def Case_12(s,a,l,r,e_deg,ve):
print("Case_12 running")
Case(s,a,l,r,e_deg,ve) ```
【问题讨论】:
标签: python string formatting boolean callable