重新启动石头、布、剪刀游戏

whhtz7ly  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(335)

初学者编写了一个石头剪刀游戏。试图习惯在我的代码中使用oop,但我无法让游戏重新启动。我在互联网上搜索以改进我的编码,我认为这是最糟糕的。请帮忙!!!这是我的密码:

import random
import sys

class User:
    # ask user for name
    def __init__(self, name):
        self.name = User_name

class Results:
    # record user input
    while True:
        def __init__(self):
            user_input = input("Pick rock, paper, or scissors?")

            self.selection = user_input

        def output(self):
            #return cpu output and run through the game

            cpu_output = random.choice(("rock", "paper", "scissors"))

            if self.selection == cpu_output:
                print(f" {User_name} it looks like we're tied")
            elif self.selection == "rock":
                if cpu_output == "paper":
                    print(f"Paper beats rock. {User_name} it looks like I won this round")
                else:
                    print(f"Great job {User_name}. Rock beats scissors it looks like you won this round")
            elif self.selection == "paper":
                if cpu_output == "scissors":
                    print(f"Scissors beats paper. {User_name} it looks like I won this round")
                else:
                    print(f"Great job {User_name}. Paper beats rock it looks like you won this round")
            elif self.selection == "scissors":
                if cpu_output == "rock":
                    print(f"Rock beats scissors. {User_name} it looks like I won this round")
                else:
                    print(f"Great job {User_name}. Scissors beats paper it looks like you won this round")

        def restart(self):
            #allow user to restart the game

            game_reset = input("Would you like to play again?")
            if game_reset == "Yes" or "yes":
                print(f"Starting up a new game {User_name}")
                rps_game.output()
            else:
                print(f"Exiting the game. Thanks for playing {User_name}")
                sys.exit()
        break

User_name = input("What is your name? ")
nbysray5

nbysray51#

正如其他人提到的,您的代码确实需要很多更改。但在这里,我向你们提供了一些开始:

import random
import sys

class User:
    # ask user for name
    def __init__(self, name):
        self.name = User_name

class Results:
    # record user input
    def __init__(self):
        user_input = input("Pick rock, paper, or scissors?")

        self.selection = user_input

    def output(self):
        #return cpu output and run through the game

        cpu_output = random.choice(("rock", "paper", "scissors"))

        if self.selection == cpu_output:
            print(f" {User_name} it looks like we're tied")
        elif self.selection == "rock":
            if cpu_output == "paper":
                print(f"Paper beats rock. {User_name} it looks like I won this round")
            else:
                print(f"Great job {User_name}. Rock beats scissors it looks like you won this round")
        elif self.selection == "paper":
            if cpu_output == "scissors":
                print(f"Scissors beats paper. {User_name} it looks like I won this round")
            else:
                print(f"Great job {User_name}. Paper beats rock it looks like you won this round")
        elif self.selection == "scissors":
            if cpu_output == "rock":
                print(f"Rock beats scissors. {User_name} it looks like I won this round")
            else:
                print(f"Great job {User_name}. Scissors beats paper it looks like you won this round")

    def restart(self):
        #allow user to restart the game

        game_reset = input("Would you like to play again?")
        if game_reset == "Yes" or "yes":
            print(f"Starting up a new game {User_name}")
            self.output()
        else:
            print(f"Exiting the game. Thanks for playing {User_name}")
            sys.exit()

User_name = input("What is your name? ")
while True:
    result = Results()
    result.output()
    result.restart()

你没有 define 使用它们的函数,您可以 call 他们。
除此之外,还有一些问题。比如,游戏永远不会停止。在哪里进行调试? if game_reset == "Yes" or "yes": 您确定这是检查连接的正确方法吗?或者你认为你遗漏了什么。是这样吗?检查并尝试找出答案。
所以是一个质疑的社会,明确的质疑。不是像这样的问题——“我的代码不起作用,修复它!”但不管怎样,你这里有些东西。

相关问题