python - I need a fresh pair of eyes for this Game -


i've been trying create rock-paper-scissor game [ sort of ;-) ], reason when computer wins doesn't display it, gives me error. read through code , tell me problem. (i new coding, , there million mistakes please explain them )

import random  class game:      def __init__(self, human):         # these possible hands , corresponding numbers         self.hand = {              1: "rock", 2: "paper", 3: "scissors",             4: "lizard", 5: "spock", 6: "spider-man",             7: "batman", 8: "wizard", 9: "gun"         }          #  here write "hand" , "what beats + wining message"         self.message = {              "rock": {"scissors": "rock crushes scissors",                      "lizard": "rock crushes lizard.",                      "spider-man": "rock knocks out spider-man.",                      "wizard": "rock interrupts wizard."},              "paper": {"rock": "paper covers rock.",                       "spock": "paper disproves spock.",                       "batman": "paper delays batman.",                       "gun": "paper jams gun."},              "scissors": {"paper": "scissors cut paper.",                          "lizard": "scissors decapitates lizard.",                          "spider-man": "scissors cut spider-man.",                          "wizard": "scissors cut wizard."},              "lizard": {"paper": "lizard eats paper.",                        "spock": "lizard poisons spock.",                        "batman": "lizard confuses batman, because looks killer croc.",                        "gun": "lizard small gun."},              "spock": {"rock": "spock vaporizes rock.",                       "scissors": "spock smashes scissors.",                       "spider-man": "spock befuddles spider-man.",                       "wizard": "spock zaps wizard."},              "spider-man": {"paper": "spider-man rips paper.", "lizard": "spider-man defeats lizard.",                            "wizard": "spider-man annoys wizard.", "gun": "spider-man disarms gun."},              "batman": {"rock": "batman explodes rock.", "scissors": "batman dismantles scissors",                        "spock": "batman hangs spock.", "spider-man": "batman scares spider-man."},              "wizard": {"paper": "wizard burns paper.", "lizard": "wizard transforms lizard.",                        "batman": "wizard stuns batman.", "gun": "wizard melts gun."},              "gun": {"rock": "gun breaks rock.", "scissors": "gun dents scissors.",                     "spock": "gun shoots spock.", "batman": "gun kills batman's mom."}          }          # human selection , computer selection         self.human = human         self.computer = random.randint(1, len(self.hand))      #  function selects winner, depending of beating hands     # winner picked assuming human default winner     # 'else' computer wins.     def show_hand(self):         print(("you played ", self.hand[self.human], " computer played ", self.hand[self.computer], "!"))       def beats(self):         if self.hand[self.human] == self.hand[self.computer]:             return "it's tie!"          elif self.hand[self.human] == "rock":             if self.hand[self.computer] == "scissors" or "lizard" or "spider-man" or "wizard":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "paper":             if self.hand[self.computer] == "rock" or "spock" or "batman" or "gun":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "scissors":             if self.hand[self.computer] == "paper" or "lizard" or "spider-man" or "wizard":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "lizard":             if self.hand[self.computer] == "paper" or "spock" or "batman" or "gun":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "spock":             if self.hand[self.computer] == "rock" or "scissors" or "spider-man" or "wizard":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "spider-man":             if self.hand[self.computer] == "paper" or "lizard" or "wizard" or "gun":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "batman":             if self.hand[self.computer] == "rock" or "scissors" or "spock" or "spider-man":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "wizard":             if self.hand[self.computer] == "paper" or "lizard" or "batman" or "gun":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"          elif self.hand[self.human] == "gun":             if self.hand[self.computer] == "rock" or "scissors" or "spock" or "batman":                 return self.message[self.hand[self.human]][self.hand[self.computer]] + " win!"             else:                 return self.message[self.hand[self.computer]][self.hand[self.human]] + " computer wins!"   def main():     while true:         intro()         while true:             instructions()             while true:                 print()                 player_input = input("please make selection: ")                 print()                  if int(player_input) == 1 or player_input.lower() == "rock":                     player_input = 1                     break                 elif int(player_input) == 2 or player_input.lower() == "paper":                     player_input = 2                     break                 elif int(player_input) == 3 or player_input.lower() == "scissors":                     player_input = 3                     break                 elif int(player_input) == 4 or player_input.lower() == "lizard":                     player_input = 4                     break                 elif int(player_input) == 5 or player_input.lower() == "spock":                     player_input = 5                     break                 elif int(player_input) == 6 or player_input.lower() == "spider-man" or player_input.lower() == \                         "spiderman":                     player_input = 6                     break                 elif int(player_input) == 7 or player_input.lower() == "batman":                     player_input = 7                     break                 elif int(player_input) == 8 or player_input.lower() == "wizard":                     player_input = 8                     break                 elif int(player_input) == 9 or player_input.lower() == "gun":                     player_input = 9                     break                 else:                     print("there seems error on selection."),                     print("please read instructions , try again.")                     print()                      instructions()              game = game(player_input)              print()             game.show_hand()             print()              print(game.beats())              if game.beats() == "it's tie!":                 print()                 print("give 1 more try!")                 print()             else:                 break              cont = input("do want play again (y/n)?\n\n: ")             if cont.lower() == "no" or "n":                 break   def intro():     print()     print("this game of rock, paper, scissors, lizard, spock,\nspider-man, batman, wizard, gun.")     print("you playing vs computer.")     print()   def instructions():     print()     print("*instructions*")     print()     print('enter "1" or "rock" rock')     print('or')     print('enter "2" or "paper" paper')     print('or')     print('enter "3" or "scissors" scissors')     print('or')     print('enter "4" or "lizard" lizard')     print('or')     print('enter "5" or "spock" spock')     print('or')     print('enter "6" or "spider-man" spider-man')     print('or')     print('enter "7" or "batman" batman')     print('or')     print('enter "8" or "wizard" wizard')     print('or')     print('enter "9" or "gun" gun')     print()  main() 

i don't think stackoverflow got indentation correctly. if want original file, here it's. https://drive.google.com/open?id=0bxbnlq7y4nlxmhp2wvf5ounqd3m

the keyerror getting indicates trying access key doesn't exist in dictionary. looking on code, seems self.messages dictionary doesn't contain 'paper' key in all of sub-dictionaries.

now, lets take , try improve code. first off, print call doesn't need confusing.

def show_hand(self):     print(("you played ", self.hand[self.human], " computer played ", self.hand[self.computer], "!")) 

can be

def show_hand(self, human, computer):     print("you played {}, computer played {}!".format(human, computer)) 

now, pass in variables method call.

further, lets address beats method. can use same process, pass in self.human , self.computer. cuts out lot of noise in code:

def beats(self, human, computer):     if human == computer:         return "it's tie!" 

now, lets address user input sanitization. ton easier! can reasonably expect either string (indicating user entered paper) or integer. why not validate input once pass game?

def __init__(self, human):     ...     if human.isdigit():         self.human = int(human)     else:         reversed = {v.lower(): k k, v in self.hand.iteritems()}         self.human = reversed.get(human.lower())         if not self.human:             raise exception("not valid choice") 

boom, done.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -