hi im trying to implement something in python that in c languages i would usually do with a case statement.
the issue is that when the inturpreter is initializing the dictionary it calls the functions in my dictionary (including quit). i think i might need something like a function pointer but im not really familiar with python yet.Code:# Enter the main program loop while 1==1: # Display the menu to the user print "Enter in a choice" print " 1) Enter a new ticker to market" print " 2) Buy a stock" print " 3) Sell a stock" print " 4) Display portfolio results" print " 5) Exit Simulation" print "Portfolio Information:" print " Cash:", portfolio.portfolioCash print "Market Information:" print " Trading day:", currentTradingDay # Allow the user to select an menu entry choice = raw_input("Menu: ") { '1': addStockDialogue(market), '2': buyStock(), '3': sellStock(), '4': displayResults(), '5': quitSimulation() }.get(choice, "Error, improper input")
i could use if and else but typically a 'case statement' is 'cleaner'. i want to write good code.
thanks for the help.
Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.
In python everything is a pointer to the C structure PyObject*.
So when the interpreters sees blabla(), it looks for an object called blabla, and the checks if this object can be called(in which case it is a function or a method).
what you need to do is just give the function name as the value, what you are doing there is giving the returned value by those functions.
so, following your logic you should do:
And yes, I hate it when it comes to this and there is no switch statement in Python. I also see that you give args at one of the function, there are two solutions to this:Code:choice = raw_input("Choice: ") func = { "1":foo, "2':bar, "3":anotherFunction, }.get(choice,"blabla") func()
-either create another dictionary which contains the args according to the choice
-or just use the stupid and classic if...elif..else structure.
note that args is a tuple and that there is a star in front of it when I pass it to func.Code:args = { "1":(market), "2":(player,enemy), }.get(choice) func(*args)
ahh i see. thank you. that makes sense. ya it would be nice to have a regular case statement. maybe im just too used to C/C++/Java.
Thanks for the help.
Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.
There are also a couple of other ways that you can call functions with arguments in this way.
1) Use an inline "lambda" function:
The keyword "lambda" makes a temporary function. In this example, the temporary function simply calls your function with a parameter.Code:choice = raw_input("Menu: ") { '1': lambda : addStockDialogue(market), '2': buyStock, }.get(choice, "Error, improper input")
2) Use a "curried function":
Currying is an advanced concept from functional programming, but it can be thought of as choosing the values of some arguments to a function without fully evaluating the function. There is a good recipe for implementing the curry function here (you would need to copy and paste this curry function code into your file before this example would work).Code:choice = raw_input("Menu: ") { '1': curry(addStockDialogue, market), '2': buyStock, }.get(choice, "Error, improper input")
Hmm, I hadn't thought about lambda.
Looking at the curry example, a simple decorator could've made the job too.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks