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

时间:2015-09-29 23:43:20

标签: python debugging python-3.4

我一直试图创建一个Rock-Paper-Scissor游戏[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)?\n\n: ")
            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

2 个答案:

答案 0 :(得分:3)

您获得的self.messages表示您正在尝试访问字典中不存在的密钥。查看您的代码,您的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))

可以

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

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

此外,让我们解决节拍方法。我们可以使用相同的过程,传入self.human和self.computer。这减少了代码中的大量额外噪音:

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") 的内容)或整数。那么为什么一旦你将输入传递到游戏中就不验证输入?

{{1}}
砰的一声,完成了。

答案 1 :(得分:0)

我发现了什么是错的,它希望我这样做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放在条件之间就足够了。