python类石头剪刀布游戏

2024-04-26 06:05:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在课堂上创造了“石头剪刀” 我在“def游戏(self)”中创建了一个if语句 你能告诉我我的代码有什么问题吗,我猜它和“elif”有关

问题是“播放器”和“计算机”的值不能在“elif”中传递

import random

possible_actions = ["rock", "paper", "scissors"]
computer = random.choice(possible_actions)


class Game_Rock_Paper_Scissors:
    def __init__(self, player, computer):
        self.player = player
        self.computer = computer


    def Game(self):
        print("Starting to play")
        if player == computer:
            print("Tie!")
        elif player == "Rock":
            if computer == "Paper":
                return "You lose!{1} covers{0}" .format(self.computer,self.player)
            else:
                return "You win!{0} smashes{1}" .format(self.player ,self.computer)
        elif player == "Paper":
            if computer == "Scissors":
                return "You lose!{1} cut {0}" .format(self.computer,self.player)
            else:
                return "You win! {0} covers {1}" .format(self.player ,self.computer)
        elif player == "Scissors":
            if computer == "Rock":
                return "You lose...{1} smashes {0}" .format(self.computer,self.player)
            else:
                return "You win!{1} cut {0}" .format(self.player ,self.computer)
        print("Game end")

if __name__ == '__main__':
    player = input("Enter a choice (rock, paper, scissors): ")

    if player!="rock" and player!="paper" and player!="scissors":
            print("That's not a valid play. Check your spelling!")

    else:
        print(f"\nYou chose {player}, computer chose {computer}.\n")
        playing = Game_Rock_Paper_Scissors(player ,computer)
        playing.Game()




Tags: selfyougameformatreturnifdefelse
1条回答
网友
1楼 · 发布于 2024-04-26 06:05:32

正如@khelwood所提到的,您的代码有两个问题

  1. 岩石、纸等的箱子
  2. 实例和局部变量的互换使用

正确的代码如下:

import random

possible_actions = ["rock", "paper", "scissors"]
computer = random.choice(possible_actions)


class Game_Rock_Paper_Scissors:
    def __init__(self, player, computer):
        self.player = player
        self.computer = computer


    def Game(self):
        print("Starting to play")
        if self.player == self.computer:
            print("Tie!")
        elif self.player == "rock":
            if self.computer == "paper":
                return "You lose!{1} covers {0}" .format(self.computer,self.player)
            else:
                return "You win!{0} smashes {1}" .format(self.player ,self.computer)
        elif self.player == "paper":
            if self.computer == "scissors":
                return "You lose!{1} cut {0}" .format(self.computer,self.player)
            else:
                return "You win! {0} covers {1}" .format(self.player ,self.computer)
        elif self.player == "scissors":
            if self.computer == "rock":
                return "You lose...{1} smashes {0}" .format(self.computer,self.player)
            else:
                return "You win!{1} cut {0}" .format(self.player ,self.computer)
        

if __name__ == '__main__':
    player = input("Enter a choice (rock, paper, scissors): ")

    if player!="rock" and player!="paper" and player!="scissors":
            print("That's not a valid play. Check your spelling!")

    else:
        print(f"\nYou chose {player}, computer chose {computer}.\n")
        playing = Game_Rock_Paper_Scissors(player ,computer)
        print(playing.Game())
        print("Game end")

相关问题 更多 >