3 回答
TA贡献1802条经验 获得超10个赞
您忘记从 中返回值switch_demo(),请检查以下代码:
def switch_demo(var):
switcher = {
0: zero,
1: one,
2: two,
3: three,
666: demon,
}
return switcher.get(var, "Invalid num")
while opened:
if opened == True:
var = int(input("enter a number between 1 and 9999999999 "))
print(switch_demo(var)))
elif opened== False:
print("Goout")
sys.exit()
TA贡献1851条经验 获得超3个赞
您必须从switch_demo 另外返回一个函数,更改print(switch_demo(var)))为print(switch_demo(var)()). 可以这样重写以更有意义:
var = "something"
function = switch_demo(var)
print(function())
如果这是您想要的,这实际上会调用function并打印出它返回的任何内容。
TA贡献1777条经验 获得超10个赞
字典值作为要调用的函数:
切换器[0] ()
def switch_demo(var):
switcher = {
0: zero,
1: one,
2: two,
3: three,
666: demon,
}
#var = switcher.get(var, "Invalid num")
switcher[int(var)]() # exec function
-
def switch_demo(var):
switcher = {
0: zero,
1: one,
2: two,
3: three,
666: demon,
}
#var = switcher.get(var, "Invalid num")
return (switcher[int(var)]())
while opened:
if opened == True:
var = int(input("enter a number between 1 and 9999999999 "))
print (switch_demo(var))
elif opened == False:
print("Goout")
添加回答
举报