2 回答
TA贡献2019条经验 获得超9个赞
您在初始化不正确的列表时正在调用函数尝试以下代码
interest_list = []
function_list = [cheese, wine, beer, spirits, \
coffee, chocolate]
for afunc in function_list :
loop_check = None
while loop_check == None :
try :
if int(afunc()) <= 5 and int(afunc()) >= -5 :
interest_list.append(afunc)
else :
raise RangeQuestionsError
except (ValueError, RangeQuestionsError) :
print(afunc, " is not a valid choice. Try again.", sep="")
loop_check = None
TA贡献1794条经验 获得超7个赞
您可以将列表作为字符串列表,然后使用eval函数对其进行评估。
请记住,您还必须定义函数。
interest_list = []
function_list = ['cheese()', 'wine()', 'beer()', 'spirits()', 'coffee()', 'chocolate()']
for func in function_list :
afunc = eval(func)
loop_check = None
while loop_check == None :
try :
if int(afunc) <= 5 and int(afunc) >= -5 :
interest_list.append(afunc)
else :
raise RangeQuestionsError
except (ValueError, RangeQuestionsError) :
print(afunc, " is not a valid choice. Try again.", sep="")
loop_check = None
添加回答
举报