-2

How could I transform this code to use one function and use a different variable depending on which variable is passed through when the function is called?

def p1win():
    p1cards.append(play1card)
    p1cards.append(play2card) 

def p2win():
    p2cards.append(play1card)
    p2cards.append(play2card)

Any help would be greatly appreciated, thanks

Masha
  • 19
  • 3
  • Can you please clarify what you mean by "use a different variable depending on which variable is passed"? Which part do you want to parameterise – ``p1cards``|``p2cards`` or ``play1card``|``play2card``? Do you know how function parameters work in general? What have you tried so far? – MisterMiyagi Jun 16 '20 at 21:09
  • Read the following answer: https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – mazhar islam Jun 16 '20 at 21:14

1 Answers1

0

You can make a function win(cards) and pass in the winner's cards.

def win(cards):
    cards.append(play1card)
    cards.append(play2card)

Then, when a player (e.g. p1) wins, simply do:

win(p1cards)

I hope this helps!

Reece Coombes
  • 394
  • 2
  • 9