1

I just started python and am trying to wrap my head around this concept of having arguments in a function but not using all of them, would like to know the logic behind this.

The code looks like this:

def player_busts(player,dealer,chips):
    print("Player busts!")
    chips.lose_bet()

It's meant to take three arguments: a player object, a dealer object, and a chips object. But why they only used the chips object passed in? Do other two play any role here?

Thank you very much.

2
  • 1
    Is this your code? If so, just remove the unused argument. Commented Jun 11, 2018 at 1:03
  • I'd say this might be more of a software engineering question, but in C++ you can also add an argument and not use it, think you can with any programming language AFAIK? But in C++ the compiler would give you an unreferenced formal parameter warning. Essentially I guess its a waste of memory, the only purpose being if you intend on using the argument in the future and just declare it as a placeholder / reminder? Commented Jun 11, 2018 at 5:59

3 Answers 3

1

This often happens with callback functions. A callback is a function will get called with certain pre-set parameters when a specified event happens.

Somewhere in a larger program, is another game-playing function that calls these.

def play():
    player = ???()
    dealer = ???()
    chips = ???()

    while something:
        if another_thing:
            action = player_wins
        elif something_else:
            action = player_busts
        elif another_thing:
            action = dealer_busts
        else:
            action = push

        action(player, dealer, chips)

Since some of these actions need to use player or dealer or chips or any combination of them, any action called would need to accept those parameters even if it doesn't use them.

If you defined player_busts like so,

def player_busts(chips):
    print("Player busts!")
    chips.lose_bet()

You'd get the following error if it was called in play. TypeError: player_busts takes exactly one argument (3 given)

Sign up to request clarification or add additional context in comments.

Comments

0

A function can take as many argument as you like to feed it, however not using it with a function is a waste. The purpose of a function is to take in arguments and do some work with them to return a argument or arguments back. In your example only chips are used therefore the other two can be removed and still have the function behave exactly the same.

3 Comments

ty, as I mentioned I just started learning python, and I encountered this in my learning course , I though the instructor coded like this for a purpose but couldnt figure out myself, that's why I need to ask here. So what you saying was this is a bad coding practice and should never be used? I wonder why they put it there in the course which costed me $20...
Of course! You are correct, this snip-it of code as is would be bad-practice. I don't have the full context of why this was part of your course but it could have been used to illustrate that even though your function has only 1 working argument, the player_busts calls for 3 non-optional arguments regardless. You could rewrite this a bit and make the function more flexible with optional arguments based on player and dealer names: 'def player_busts(player="Player", dealer="Dealer", chips): print('{} busts! {} wins!.format(player, dealer) chips.lose_bet()' now all arguments are used
and the function can now be called with a specified player and dealer name + chips, or just call it with chips player_busts(player="player1", dealer="BossMan", chips=100) player_busts(chips=100)
0

This is not a complete code sample, specially if your instructor has provided you with it. More functionality will be added to it as the course goes on, to show you how a full program is built step by step. You have most probably jumped the gun and posted this partial code here.

Firstly, The function is not returning anything, which means it is evaluating something and it stops there, the result of evaluation cannot be used anywhere. Which basically means more statement will be added to this function till your reach the return statement.

Secondly, chips.lose_bet() is a statement calling a function .lose_bet() which is also not defined in this sample.

Third, the arguments in this function might as well be functions themselves that will be defined later; and when they will be defined the instructor, they may be called from this function. There are many possibilities in ways similar to this.

These few points are bigger holes in the above code than having extra arguments. Which basically means you have actually jumped the gun in trying to understand what is going on here. So continue with your course and you'll see.

3 Comments

ok, here I copy pasted the whole course project the instructor given:
ok, here I copy pasted the section in question, its from the 'solution' page, means a finished project: def player_busts(player,dealer,chips): print("Player busts!") chips.lose_bet() def player_wins(player,dealer,chips): print("Player wins!") chips.win_bet() def dealer_busts(player,dealer,chips): print("Dealer busts!") chips.win_bet() def dealer_wins(player,dealer,chips): print("Dealer wins!") chips.lose_bet() def push(player,dealer): print("Dealer and Player tie! It's a push.")
@stackken Sorry, if you have any additional code you should edit your post and add it. There was no information that original code is part of a problem and this is the solution. You should revisit your course more carefully and try to read the question properly. if you still have a problem, you should better frame it and ask here. Till then this question is better off closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.