mikael
mikael

Reputation: 41

Can I use arguments to reference a variable?

def rock_paper_scissors_lizard_spock(player1, player2):

    # each variable stores what it can defeat
    scissors = ['paper','lizard']
    rock = ['lizard', 'scissors']
    paper = ['rock','spock']
    lizard = ['spock','paper']
    spock = ['scissors','rock']


    if str(player2) in player1:
        print('player 1 wins')


rock_paper_scissors_lizard_spock('rock','spock')

Can I use the arguments to the function declared above to refer to a particular variable?

Upvotes: 0

Views: 37

Answers (1)

jpp
jpp

Reputation: 164623

Short answer, no. Use a dictionary.

def rock_paper_scissors_lizard_spock(player1, player2):

    # each variable stores what it can defeat
    mapper = {'scissors': ['paper', 'lizard'],
              'rock': ['lizard', 'scissors'],
              'paper': ['rock', 'spock'],
              'lizard': ['spock', 'paper'],
              'spock': ['scissors', 'rock']}

    print('player %s wins' %(1 if player2 in mapper[player1] else 2))

rock_paper_scissors_lizard_spock('paper', 'rock')  # player 1 wins

Upvotes: 2

Related Questions