只有打印输出的Python函数?

时间:2015-12-14 02:46:48

标签: python

我正在尝试在python中创建一个输出如下菜单的函数:

MENU

p)玩匹配的便士游戏

q)退出

我尝试过的......

def display_menu():
    print "###MENU###"
    print "p) Play the matching penny game"
    print "q) Quit"

我想在脚本中调用该函数并显示菜单,但它不会显示在shell中,也不会抛出错误。我该怎么做呢?

4 个答案:

答案 0 :(得分:1)

我猜你忘了调用display_menu函数。试试这个:

def display_menu():
    print "MENU".center(10, "#")
    print "p) Play the matching penny game"
    print "q) Quit"

if __name__ == "__main__":
    display_menu()

答案 1 :(得分:0)

def display_menu():
    print "###MENU###"
    print "p) Play the matching penny game"
    print "q) Quit"

display_menu()    # call the function

你必须实际调用该函数才能运行它。

答案 2 :(得分:0)

你的文字周围可能需要括号..

def display_menu():
    print ("###MENU###")
    print ("p) Play the matching penny game")
    print ("q) Quit")
display_menu()

答案 3 :(得分:0)

您编写的功能100%可以满足您的需求。问题是你在执行期间根本没有调用它。如果你的程序变得复杂,我建议创建一个main函数,其中包含所有主要内容,程序的核心,并通过以下方式调用:

if __name__ == "__main__":
    main()

否则你可以随意调用它,只要它在运行时执行,它当前不是。