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")
添加回答
舉報