这场比赛我需要一双新鲜的眼睛



我一直在尝试创建一个石头剪刀布游戏[Sort of;-)],但由于某种原因,当计算机获胜时它不显示它,它给了我一个错误。谁能把代码读一遍,告诉我是什么问题。(我对编码很陌生,可能有一百万错误,所以请解释清楚)

import random
class Game:
    def __init__(self, human):
        # These are the possible hands and their corresponding numbers
        self.hand = {
            1: "Rock", 2: "Paper", 3: "Scissors",
            4: "Lizard", 5: "Spock", 6: "Spider-Man",
            7: "Batman", 8: "Wizard", 9: "Gun"
        }
        #  Here we write the "Hand" and "What it 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 he looks like Killer Croc.",
                       "Gun": "Lizard is too small for the 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 the 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 the Gun."},
            "Gun": {"Rock": "Gun breaks Rock.", "Scissors": "Gun dents Scissors.",
                    "Spock": "Gun shoots Spock.", "Batman": "Gun kills Batman's mom."}
        }
        # human selection and computer selection
        self.human = human
        self.computer = random.randint(1, len(self.hand))
    #  This function selects a winner, depending of beating hands
    # The winner is picked by assuming the human is the default winner
    # 'else' the computer wins.
    def show_hand(self):
        print(("You played ", self.hand[self.human], " the computer played ", self.hand[self.computer], "!"))

    def beats(self):
        if self.hand[self.human] == self.hand[self.computer]:
            return "It's a 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The 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]] + " You Win!"
            else:
                return self.message[self.hand[self.computer]][self.hand[self.human]] + " The Computer Wins!"

def main():
    while True:
        intro()
        while True:
            instructions()
            while True:
                print()
                player_input = input("Please make your 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 to be an error on your selection."),
                    print("Please read the instructions well and try again.")
                    print()
                    instructions()
            game = Game(player_input)
            print()
            game.show_hand()
            print()
            print(game.beats())
            if game.beats() == "It's a tie!":
                print()
                print("Give it one more try!")
                print()
            else:
                break
            cont = input("Do you want to play again (Y/N)?nn: ")
            if cont.lower() == "no" or "n":
                break

def intro():
    print()
    print("This is a game of Rock, Paper, Scissors, Lizard, Spock,nSpider-Man, Batman, Wizard, Gun.")
    print("You will be playing vs the Computer.")
    print()

def instructions():
    print()
    print("*Instructions*")
    print()
    print('Enter "1" or "Rock" for Rock')
    print('or')
    print('Enter "2" or "Paper" for Paper')
    print('or')
    print('Enter "3" or "Scissors" for Scissors')
    print('or')
    print('Enter "4" or "Lizard" for Lizard')
    print('or')
    print('Enter "5" or "Spock" for Spock')
    print('or')
    print('Enter "6" or "Spider-Man" for Spider-Man')
    print('or')
    print('Enter "7" or "Batman" for Batman')
    print('or')
    print('Enter "8" or "Wizard" for Wizard')
    print('or')
    print('Enter "9" or "Gun" for Gun')
    print()
main()

我不认为StackOverflow得到我的缩进正确。如果你想要原始文件,在这里。https://drive.google.com/open?id=0BxbNlq7y4nLxMHp2WVF5OUNqd3M

您获得的KeyError表示您正在尝试访问字典中不存在的键。查看您的代码,似乎您的self.messages字典没有在所有子字典中包含'Paper'作为键。

现在,让我们看看并尝试改进这段代码。首先,print调用不需要这么混乱。

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

可以

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

现在,只需将变量传递给方法调用。

进一步,让我们来处理beats方法。我们可以使用相同的过程,传入self。人类和自我,电脑。这样可以在代码中省去很多额外的噪音:

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

现在,让我们处理用户输入处理。这可能会容易得多!您可以合理地期望字符串(表明用户输入了paper之类的东西)或整数。所以为什么不在将输入传递到游戏中时验证它呢?

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

繁荣,做。

我发现了什么是错误的,它想让我写这样的if语句

    `if self.hand[self.computer] == "Rock" or self.hand[self.computer] == "Scissors" or self.hand[self.computer] == "Spider-Man" or self.hand[self.computer] == "Wizard":
            return self.message[self.hand[self.human]][self.hand[self.computer]] + " You Win!"
        else:
            return self.message[self.hand[self.computer]][self.hand[self.human]] + " The Computer Wins!"`

我认为把or放在两个条件之间就足够了。

最新更新