dimanche 22 août 2021

How do I modify the if statement?

I was trying to do a very compact rps(rock, paper, scissors) and to make it work I used a very inefective but funcitonal logic that if the result of the variables was a certain value then it will win the p if not (this includes the draw) it will lose. More explanation at the bottom of the code, but I wanted to be if the value p and m passed through the diccionary table euqualed to 1 or 2 then it will execute the next line of code, but with the brakets it worked half, without it missed another types of operations and with the brakets in the expresion on the right of the == it missed another part. I noticed that it works if you put it like another expression like:

if table[p]-table[m] == 1 or table[p] - table[m] == -2

but since I wanted to be compact, i was wondering if there was any way to put it in the same expression, thanks. PS: the variables have been wirtten with its full names to make it more understandable, this will not be the case in the final version.

    table = {'r':0, 'p':1, 's':2}
    p = input()
    m = input()
    if table[p]-table[m] == (1 or -2):
        print('w')
    
    else:
        print('l')
    
    '''
    s,p= 1w
    s,r=2l
    p,r =1w
    p,s =-1l
    r,s=-2w
    r,p=-1l
    
    w=1,1,-2
    l=-1,-1,2'''

Aucun commentaire:

Enregistrer un commentaire