Python Forum

Full Version: Python calling function unexpectedly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry for the long python code!
I'm trying to optimise this code, so I thought I'd make the if lists into a callable function. However, whenever I try to call it (last line), it runs basic_option() instead. It's nothing to do with the code in if_tree() itself, because if I comment it all out the same thing still happens. However, if I replace the list of functions with something else, it works.
Basically the problem is python is calling the function in the call for if_tree unexpectedly.

Any advice on the code in general would be great as well!

If anyone can help that would be great!
Thanks in advance.
When setting a variable or sequence index to a function, do not use parentheses. Parentheses signify that the function should be called. If you use the function name by itself, the variable or index is instantiated to the function itself.

if_tree(
    MAIN_TEXT, 
    ["1", "2"], 
    ["Q", "H", "R"], 
    [basic_option, advanced_option, quit_program, display_help, exec("restart_calculator()")]
)
Other than that, I believe this script should be refactored into a class called Calculator. The function input() returns a string so there is no need for str(input()).