Home > Back-end >  How do I use more than one criteria in a dictionary
How do I use more than one criteria in a dictionary

Time:10-20

This is a snippet of code where I am trying to set the rules for rock paper scissors

table = {"paper" and "paper" : "Draw",
         "paper" and "rock" : "Win",
         "paper" and "scissors" : "You Lose",
         "rock" and "paper" : "You Lose",
         "rock" and "rock" : "Draw",
         "rock" and "scissors" : "Win",
         "scissors" and "paper" : "Win",
         "scissors" and "rock" : "You Lose",
         "scissors" and "scissors" : "Draw"}

Here I'm trying to send the result of the rock paper scissors and the randomly chosen value from the rps list

@client.command()
async def rockpaperscissors(ctx, rpschoice):
  x= random.choice(rps)
  await ctx.send(x)
  await ctx.send(table[rpschoice and x])

Here is the rps list

rps=["rock","paper","scissors"]

The program produces to me completely random results which I think is a result of the dictionary being wrong. If the program would work it would give the value the computer has chosen and the result of the game. Any help would be much appreciated.

CodePudding user response:

If you want to keep a data structure that is a dictionary with "2 keys", as it were, you can go with a tuple. Tuples are hashable, and therefore can be a key of a dict:

table = {("paper", "paper") : "Draw",
         ("paper", "rock") : "Win",
         ("paper", "scissors") : "You Lose",
         ("rock", "paper") : "You Lose",
         ("rock", "rock") : "Draw",
         ("rock", "scissors") : "Win",
         ("scissors", "paper") : "Win",
         ("scissors", "rock") : "You Lose",
         ("scissors", "scissors") : "Draw"}

You can then refer to it as:

result = table[('paper', 'rock')]

Note that there are many other ways to solve this, I'm just trying to come up with a data structure that is similar to the question.

  • Related