TrickJarrett.com

2/13/2021 1:31 pm |

I am working on learning Python as a programming language and getting used to it. I sat down and coded "Rock, Scissors, Paper, Lizard, Spock" - First I did the endless if, elif, else statements then decided to go for a more compact solution via a dense multidimensional array.

from random import randint

t = [["Rock",[0,-1,1,1,-1],["ties","is covered by","smashes","crushes","is thrown by"]],
   ["Paper",[1,0,-1,-1,1],["covers","ties","is cut by","is eaten by","disproves"]],
   ["Scissors",[-1,1,0,1,-1],["smashed by","cuts","ties","decapitates","is used by"]],
   ["Lizard",[-1,1,-1,0,1],["is crushed by","eats","is decapitated by","ties","poisons"]],
   ["Spock",[1,-1,1,-1,0],["throws","is disproven by","uses","is poisoned by","ties"]]]

#assing a random play to computer
computerindex = randint(0,4)

player = False;

while player == False:
   player = input("Rock, Paper, Scissors, Lizard, Spock? ")

   noresult = True
   for entry in t:
       if entry[0] == player:
           if entry[1][computerindex] > 0:
               noresult = False
               print("You win! ",player,entry[2][computerindex],t[computerindex][0])
           elif entry[1][computerindex] 

📧 Newsletter

Did you know that you can sign up for the email newsletter of this blog? Get an email with the day's posts delivered to your inbox! Sign up here.