Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 11th, 2013, 03:29 AM
Latsabb Latsabb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Latsabb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 55 sec
Reputation 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!

Reply With Quote
  #2  
Old February 11th, 2013, 04:19 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 138 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 9 h 8 m 33 sec
Reputation Power: 133
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

Reply With Quote
  #3  
Old February 11th, 2013, 04:57 AM
Latsabb Latsabb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Latsabb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 55 sec
Reputation 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()

?

Reply With Quote
  #4  
Old February 11th, 2013, 05:11 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 138 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 9 h 8 m 33 sec
Reputation Power: 133
Quote:
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

Reply With Quote
  #5  
Old February 11th, 2013, 05:17 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 138 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 9 h 8 m 33 sec
Reputation Power: 133
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.

Reply With Quote
  #6  
Old February 11th, 2013, 06:07 AM
Latsabb Latsabb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Latsabb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 55 sec
Reputation 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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Question about calling functions.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap