Python,将菜单连接到函数

时间:2016-10-07 15:21:17

标签: python

我有一个菜单和一些功能可以继续使用。我的任务是添加更多功能。现在的菜单代码看起来像这样。

def menu():

"""
Display the menu with the options that The Goddamn Batman can do.
"""
print(chr(27) + "[2J" + chr(27) + "[;H")
print(meImage())
print("Hi, I'm The Goddamn Batman. There is nothing I can't do. What can I do you for?")
print("1) Present yourself to The Goddamn Batman.")
print("2) Tell me your age. I'm going to show you a neat trick.")
print("q) Quit.")

问题是print2。我得到了在Cygwin中出现的替代方案,但是当我按下“2”时,我收到错误。功能(不完整)如下所示:

def Age():
"""
Ask the user's age and calculate for how many seconds the user has lived
"""

age = input("How old are you? ")
    if choice == int:
        print ("Ah, %s, that is old." % age)
    result = (input * (int)31 556 926)
    return result
    print("You know what? Your age actually makes ",result, "seconds.")

    else:
        print("That is not a valid choice. Please write a number.")

以前的功能,打印1(名称)和打印q(退出一切),工作正常。例如,print 1看起来像这样:

def myNameIs():
    """
    Read the users name and say hello to The Goddamn Batman.
    """
    name = input("What is your name? ")
    print("\nThe Goddamn Batman says:\n")
    print("Hello %s - you're something special." % name)
    print("What can I do for you?")

为什么打印2(年龄)不会响应?其他的工作正常,但我不能添加更多的功能,让他们工作。希望这有助于其他人学习如何正确地将功能连接到菜单。我只是卡住了。

所以,澄清一下,我的问题是函数def myNameIs():作为Cygwin中的菜单选项响应。函数def Age():没有。我不知道为什么。

在此先感谢,我非常感谢您提供的任何帮助。

编辑:

好的,请求后。这是所有代码。我很抱歉没有提供一个小提琴,但我不知道我可以用于python的任何小提琴。我只找到jsfiddle,它似乎没有任何意义上的python。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Marvin with a simple menu to start up with.
Marvin doesnt do anything, just presents a menu with some choices.
You should add functinoality to Marvin.

"""


def meImage():
    """
    Store my ascii image in a separat variabel as a raw string
    """
    return r"""
       _==/          i     i          \==_
     /XX/            |\___/|            \XX\
   /XXXX\            |XXXXX|            /XXXX\
  |XXXXXX\_         _XXXXXXX_         _/XXXXXX|
 XXXXXXXXXXXxxxxxxxXXXXXXXXXXXxxxxxxxXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 XXXXXX/^^^^"\XXXXXXXXXXXXXXXXXXXXX/^^^^^\XXXXXX
  |XXX|       \XXX/^^\XXXXX/^^\XXX/       |XXX|
    \XX\       \X/    \XXX/    \X/       /XX/
       "\       "      \X/      "      /"
    """


def menu():
    """
    Display the menu with the options that The Goddamn Batman can do.
    """
    print(chr(27) + "[2J" + chr(27) + "[;H")
    print(meImage())
    print("Hi, I'm The Goddamn Batman. There is nothing I can't do. What can I do for you?")
    print("1) Present yourself to The Goddamn Batman.")
    print("2) Tell me your age. I'm going to show you a neat trick.")
    print("q) Quit.")


def myNameIs():
    """
    Read the users name and say hello to The Goddamn Batman.
    """
    name = input("What is your name? ")
    print("\nThe Goddamn Batman says:\n")
    print("Hello %s - you're something special." % name)
    print("What can I do for you?")

def Age():
    """
    Ask the user's age and calculate for how many seconds the user has lived
    """

    age = input("How old are you? ")
        if choice == int:
            print ("Ah, %s, that is old." % age)
        result = (input * (int)31 556 926)
        return result
        print("You know what? Your age actually makes ",result, "seconds.")

        else:
            print("That is not a valid choice. Please write a number.")





def main():
    """
    This is the main method, I call it main by convention.
    Its an eternal loop, until q is pressed.
    It should check the choice done by the user and call a appropriate
    function.
    """
    while True:
        menu()
        choice = input("--> ")

        if choice == "q":
            print("Bye, bye - and welcome back anytime!")
            return

        elif choice == "1":
            myNameIs()

        else:
            print("That is not a valid choice. You can only choose from the menu.")

        input("\nPress enter to continue...")



if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

您将年数转换为秒数?

1)在python中int()是一个带参数的函数,它不会像(int)31 556 926一样投射值。 2)整数值中没有空格
3)您不能在return语句之后放置代码并期望它运行
4)choice函数中可能无法访问Age。您可以将其添加为参数
5)使用int而不是isinstance(choice, int)检查==,因为您要检查变量的类型,而不是int阶级本身。

def Age():
    """
    Ask the user's age and calculate for how many seconds the user has lived
    """

    age = input("How old are you? ")
    print("Ah, %s, that is old." % age)
    result = int(input) * 31556926  # Fixed this line
    print("You know what? Your age actually makes ",result, "seconds.")
    return result   # Moved this line down

关于您的菜单,您从未执行过您的功能或提示input,但这是您可以做的诀窍

menu_funcs = {'1': myNameIs, '2': Age} # map the options to functions
while True:
    menu()
    choice = input("Enter an option: ")
    if choice == 'q':
        return # exit
    if choice in menu_funcs:
       menu_funcs[val]() # call the function
    else:
       print("That is not a valid choice. You can only choose from the menu.")