【问题标题】:Rock Paper Scissors Not Printing Out Results石头剪刀布不打印结果
【发布时间】:2020-06-10 02:19:02
【问题描述】:

我正在尝试针对计算机创建一个石头剪刀布程序,但遇到了一些问题。

我做了两种方法,一种是电脑选择,一种是用户选择。对于计算机,它随机生成 0,1,2 并从我声明为局部变量 rps 的数组中选择石头、纸或剪刀。当我尝试使用game_play() 方法运行游戏时,我可以输入输入,但无法在玩家和计算机之间获得结果输出。

class RockPaperScissors():
    global rps
    rps = ['rock', 'paper','scissors']

    def computer_choice(self): #computer randomly picks rock paper or scissors
        x = random.randint(0,2)
        w = rps[x]
        return w
        #print(rps[x])

    def player_choice(self): #does the player choose rock paper or scissors
        x = True 
        while x:
            choice = (input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: "))
            if choice.lower() == 'rock':
                    return 'rock'
            elif choice.lower() == 'paper':
                    return 'paper'
            elif choice.lower() == 'scissors':
                    return 'scissors'
            else:
                ("please enter in rock, paper or scissors")

    def game_play(self):
        rock = RockPaperScissors()
        user = rock.player_choice()
        comp= rock.computer_choice()

        if comp == 'rock' and user == 'paper':
            return "the player wins!"
        elif comp == 'rock' and user == 'scissors':
            return "the computer wins!"
        elif comp == 'paper' and user == 'rock':
            return "the computer wins!"
        elif comp == 'paper' and user == 'scissors':
            return "the player wins!"
        elif comp == 'scissors' and user == 'paper':                        
            return"the computer wins!"
        elif comp == 'scissors' and user == 'rock':
            return "the player wins"

我正在尝试以这种方式对其进行测试:

rock = RockPaperScissors()
rock.game_play()

【问题讨论】:

  • 当 comp 和 user 选择相同时会发生什么?您需要更多 elifelse 才能在平局时捕捉到...而且在您的代码中我也没有看到打印,这就是没有输出的原因
  • 我首先注意到您的 game_play 函数实例化了您称为“rock”的对象的另一个实例,而不是在测试代码中使用那个实例。我建议从你的 game_play 函数中取出 rock=RockPaperScissors() 并为变量 user 和 comp 使用 self.player_choice() 等。
  • 我相信泰勒是正确的。我给了你的代码几次尝试并没有得到任何回报,但最终确实得到了一个对象。 (您将要打印到屏幕上)。我只是假设我一无所获都是在被捆绑的时候。
  • @CogitoErgoCogitoSum 这是有道理的。当函数返回 None 时,交互式 shell 不会打印任何内容,而当它到达没有 return 的函数末尾时会打印任何内容。

标签: python


【解决方案1】:

如果您将其作为脚本而不是 Python 解释器中的所有内容运行。您必须显式打印该值。

一种可能的解决方案是:

rock = RockPaperScissors()
print(rock.game_play())

【讨论】:

    【解决方案2】:

    直接回答您的问题:您不是在“打印”游戏,而是...

    print(rock.game_play())
    

    不过,我还需要做一些其他的改进。

    • 避免使用全局变量,以便更清楚地了解 编码。也许在函数内部定义 rps 是一个更好的主意 computer_choice() 而不是将其作为全局变量。
    • 您可以将 player_choice() 简化为:
    x = input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: ").lower()
    while x != 'rock' and x != 'paper' and x != 'scissors':
        choice = (input("Player- would you like rock, paper, or scissors? enter rock, paper, or scissors?: "))  
        x = input("please enter in rock, paper or scissors").lower()
    return x
    
    • 可能是您的代码未正确复制,但第一行以下的所有内容(RockPaperScissors 类:)都缺少一个缩进。

    最后,另一种可能的解决方案是在函数 game_play() 中打印游戏结果,而不是返回字符串。

    祝你好运,继续学习。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多