SO with the 1st snippet:
Regarding the snippet you wrote:
why not just say:
def foo(bar): a = 'moo' print(a) bar() def bar(): print('bar function') if __name__=='__main__': foo(bar) else: print('choosing not to run')since both foo() and foo(bar) work to execute the script...is it simply a matter of best practices to just use foo(bar) to run the script? Technically, whether or not I have a parameter doesn't seem too matter...
Regarding the snippet you wrote:
import random def coin_flip(): return random.choice(('head', 'tail')) def user_choice(): command = input("Choose Head or Tail: ").lower() return command def result(coin_flip, user_choice): ''' Doc string explain what function do''' coin = coin_flip() choice = user_choice() if coin == choice: print(f'\nComputer <{coin}> You gussed <{choice}> which is correct\n') else: print(f'\nComputer <{coin}> You gussed <{choice}> which is wrong\n') def menu(): while True: print('(1) Coin flip game') print('(Q) Quit\n') choice = input('Enter your choice: ').lower() if choice == '1': result(coin_flip, user_choice) elif choice == 'q': return False else: print(f'Not a correct choice: {choice}') if __name__ == '__main__': menu()user_choice() clearly creates a variable, command, and gives it a value but necessitates no parameter. Yet, result(*args) also creates variables,coin and choice, and gives them the returned value of the 2 executed functions, coin_flip and user_choice.
why not just say:
... def result(): ''' Doc string explain what function do''' coin = coin_flip() choice = user_choice() ...the logic works fine for
... def user_choice(): #yes I know value of command is set to user's input not the return result of a previous function command = input("Choose Head or Tail: ").lower() return command ...Question boils down to : Why do I need to set up parameters at all. user_choice() shows that I can create variables without parameters (technically just 1 positional)? this is where I get tripped up...