Python Forum
setting parameters for functions and nested functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setting parameters for functions and nested functions
#5
SO with the 1st snippet:
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...
Reply


Messages In This Thread
RE: setting parameters for functions and nested functions - by mepyyeti - Feb-25-2018, 03:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using dir_fd=None in some functions Skaperen 2 254 Jun-14-2024, 07:22 PM
Last Post: Gribouillis
  two functions working in a strange way zapad 2 414 May-02-2024, 01:35 PM
Last Post: zapad
  Passing writable arguments to functions. Assembler 11 1,356 Jan-15-2024, 11:32 PM
Last Post: sgrey
  partial functions before knowing the values mikisDeWitte 4 784 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Calling functions by making part of their name with variable crouzilles 4 1,017 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  __name__ and __main__ in functions Mark17 3 937 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  How can i combine these two functions so i only open the file once? cubangt 4 1,099 Aug-14-2023, 05:04 PM
Last Post: snippsat
  It seems you have to define functions at the top 357mag 7 1,532 May-10-2023, 03:01 PM
Last Post: jefsummers
  Merge two functions to one SamLiu 4 1,278 May-05-2023, 01:36 PM
Last Post: SamLiu
  Can I get some clarification on importing functions from external files. wh33t 3 1,026 Feb-25-2023, 08:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020