简体   繁体   中英

Passing through variables through functions Python

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

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 (eg p1) wins, simply do:

win(p1cards)

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM