#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Question about calling functions.


    Hello. I used to script several years ago, and I would typically use 'goto' to jump to a different function. The script would basically have a large set of functions, with a starting function that would then call a different function based on what was done or typed, and then continue jumping from there.

    I am having a bit of trouble now that I am trying out Python (and rusty on top of that) with being able to hop like that. Basically, I am currently trying to make something like this:

    def start_game():
    Welcome! Please type a command. For a list of commands, please type 'command'.

    if input=command
    -------goto list_commands
    elif input=start
    -------goto game_init
    start_game()

    def list_commands()
    Here is the list of command:
    Start
    Blah
    Blah
    Blah
    goto start_game
    list_commands()

    def game_init():
    game_init()


    Obviously this is not the syntax used, but it was just to quickly demonstrate what I mean. It asks for input, you give input, and then it calls a function based on that. In the "list_commands" function, you would get the list, and then it would return you back to the "start_game" function, so that you could then enter the commands.

    I am curious how to get this to work, as various places that I have read about calling functions dont seem to make sense, or they dont work in the way I expect them to. Thanks!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    It seems to you want to call functions based on their names or any corresponding input parameter. May be something like the following example (just for an idea)

    Code:
    import sys
    
    def f1():
        print("f1 was called")
        
    def f2():
        print("f2 was called")
        
    def f3():
        print("f3 was called")
    
    def main():
        command = raw_input("Please enter the command to run: ")
        commandDictionary = {"f1":f1, "f2":f2, "f3":f3}
        if command in commandDictionary:
            commandDictionary[command]()
        else:
            print("invalide command")
            sys.exit(-1)
        
    main()

    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Ok, so there is no need to tag it with a goto, or anything like that? You just list the function with () after it, and it automatically goes to that function?

    As in, instead of:

    goto function1 ()

    It is just a line with:

    function1()

    ?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by Latsabb
    Ok, so there is no need to tag it with a goto, or anything like that? You just list the function with () after it, and it automatically goes to that function?
    If function1 refers to a valid function name in the current module then function1() calls that function. This is precisely what I did in my example, in python you can assign functions to variables (which are in fact objects). So the values in my dictionary are in fact function type objects

    Regards,
    Dariyoosh
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    In addition as far as I know there is no goto operator in python and I don't really know (even in other programming languages such as C, Java, ...) why someone may want to use that, IMHO it makes the code less readable.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Alright, I got it. Thank you very much. There was always a command that called a function when I scripted, so I was used to having that. Also, all functions needed end tags, and it seems that this isnt the case in Python, and actually messed things up a bit for me. But now I am on my way. Thank you again!

IMN logo majestic logo threadwatch logo seochat tools logo