【发布时间】:2016-08-14 22:59:07
【问题描述】:
我知道 Python 没有像 C++ 这样的 switch case,但我正在尝试弄清楚如何去做并找到了一些东西,但不确定它是否正确实现。
所以我有类似的东西:
def choiceone():
choiceone statement here
def choicetwo():
choicetwo statement here
def switching(x):
switcher= {1: choiceone(),
2: choicetwo(),
}
func = switcher.get(x, 0)
return func()
def main():
user_input=input("Choice: ")
switching(user_input)
main()
它提示用户输入很棒,但无论我写什么数字,它总是运行choiceone。
我正在尝试了解如何根据用户选择调用函数。
【问题讨论】:
-
在
dict中使用函数时不要调用函数,例如:1: choiceone()- 只需使用函数名...1: choiceone... -
发布的代码似乎有缩进问题,语法不正确。
标签: python