我刚刚开始编程并正在尝试学习 Python。第四章的最后一部分,练习 5,how to think like a computer scientist
难倒我。我正在尝试修改 shell 脚本,以便用户可以输入“a”、“b”或“c”,并且正确的响应将根据用户的选择打印出来。到目前为止,这就是我实现它的方式,并希望有人能告诉我我在这里缺少什么。
def dispatch(choice):
if choice == 'a':
function_a()
elif choice == 'b':
function_b()
elif choice == 'c':
function_c()
else:
print "Invalid choice."
def function_a():
print "function_a was called ..."
def function_b():
print "function_b was called ..."
def function_c():
print "function_c was called ..."
dispatch1 = raw_input ("Please Enter a Function.")
print dispatch(choice)
当我运行它时,我得到名称选择未定义错误。我试图让它吐回function_b被调用......当它被输入到raw_input时。
感谢您的任何帮助,
约翰