MrBlubbintosh
MrBlubbintosh

Reputation: 87

Python, connect menu to functions

I have got a menu and a couple of functions to continue working with.My task is to add more functions. The menu code right now, looks like this.

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.")

The problem is print2. I get the alternative to show up in Cygwin but I when I press "2", I get an error. The function (not complete) looks like this:

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.")

The previous functions, print 1 (name) and print q (the quit everything one), works fine. For example, print 1 looks like this:

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?")

Why won't the print 2 (age) respond? The other ones work fine but I just can't add more functions and get them to work. Hopefully this helps other people to learn how to properly connect functions to a menu. I'm just stuck.

So, to clarify, my problem is that the function def myNameIs(): responds as a menu option in Cygwin. The function def Age(): doesn't. I don't know why.

Thanks in advance, I'm very grateful for any help you can provide.

EDIT:

Ok, after request. This is ALL the code there is. I'm very sorry for not providing just a fiddle but I don't know any fiddles I can use for python. I only find jsfiddle and it doesn't seem to have anything meant for python in it.

#!/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()

Upvotes: 0

Views: 3356

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191844

You are converting years to seconds?

1) In python int() is a function that takes a parameter, it doesn't cast values like (int)31 556 926
2) integer values don't have spaces in them
3) You can't put code after a return statement and expect it to run
4) choice is not probably not accessible in the Age function. You could add it as a parameter
5) Check for int with isinstance(choice, int) instead of ==, because you want to check for the type of the variable, not against the int class itself.

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

Regarding your menu, you never executed your functions, or prompted for input, but here's a trick you can do

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.")

Upvotes: 1

Related Questions