【问题标题】:Rock-Paper-Scissors python script failsRock-Paper-Scissors python脚本失败
【发布时间】:2023-03-26 10:45:01
【问题描述】:

我是 python 的初学者。下面是我的石头剪刀布脚本。 预期的输出是根据规则抛出“你赢”或“你输”或“平局”。 但是,即使按照以下给出的逻辑,我实际上赢了,它也会抛出“你输了”。

我哪里出错了?有人可以在这里指导我吗?

程序:

# Rock Paper and Scissors Game

import random

rock = "????"
paper = "????"
scissors = "✂"
list = [rock , paper, scissors]

player_chooses = input("What do you choose ? ").lower()

computer_chooses = random.choice(list)

print("The computer choose :", computer_chooses)

if player_chooses == rock and computer_chooses == scissors:
    print("You Win")
elif player_chooses == paper and computer_chooses == rock:
    print("You Win")
elif player_chooses == scissors and computer_chooses == paper:
    print("You Win")
elif player_chooses == computer_chooses:
    print("Draw")
else:
    print("You Lose")

【问题讨论】:

  • player_chooses 是类似'rock' 的字符串,而不是 unicode 符号。
  • 您的用户必须输入 Unicode 代码,这并不容易。使用list 作为变量名也是一个坏主意。
  • 嗯,你可以要求玩家弄清楚如何正确输入 Unicode 字符串,但这可能会使游戏更难玩。

标签: python python-3.x


【解决方案1】:

我知道我迟到了(确切地说是发布后 1 个月),但您的问题是您正在将一个字符串(就代码而言是一个字符数组)与代表每个字符串的字符串进行比较您使用的 Unicode 符号 ༼ つ ◕_◕ ༽つ。

但是我喜欢你使用这些符号的想法,所以我们为什么不将它们保存在一个函数中,该函数将根据 random.choice() 函数在正确的时间返回所选符号?

import random

def convert(string):
    if string == 'rock':
        return "?"
    elif string == "paper":
        return "?"
    elif string == "scissors":
        return "✂"
    else:  
        print("An error has raised", "Program will Stop")
        exit()

choices = ["rock" , "paper", "scissors"]

player_chooses = input("What do you choose ? ").lower()

computer_chooses = random.choice(choices)

print("The computer choose :", convert(computer_chooses))

# --- Watch tip below the code --- #

if player_chooses == computer_chooses:
    print("Draw")
elif player_chooses == "rock" and computer_chooses == "scissors":
    print("You Win")
elif player_chooses == "paper" and computer_chooses == "rock":
    print("You Win")
elif player_chooses == "scissors" and computer_chooses == "paper":
    print("You Win")
else:
    print("You Lose")

Giving a little tip here that you should keep in mind (⌐■_■): 

逻辑选择很昂贵(如果您使用的代码会花费大量时间,那真的很昂贵)当然,您几乎没有逻辑选择,但您应该注意编写它们的顺序,因为这将是顺序他们将被制造出来。

在这里,就概率而言,您有 1/4 的机会获得任何结果,因此最有效的编码方法是首先要求唯一一个只有一个比较的选项(这就是为什么我把它从最后一个位置到第一个)。

正如我之前所说,这只是一个注释,这样您就可以为您编写的下一个代码学习更好的实践! (进行这些比较的一种计算效率更高的方法是实现 switch,我鼓励您在 google 上搜索如何在 python 中使用开关,因为它们不是内置语言,但很少有聪明的方法可以通过字典来使用它们!)

祝你好运,继续战斗。 Python 是一门了不起的语言,我相信你会喜欢的!

【讨论】:

    【解决方案2】:

    我不太明白您为什么要为石头、纸、剪刀添加这些图标,因为控制台不会打印出图片供您仅从您粘贴的图标中选择,所以这些可能是错误。也许只使用一个简单的字符串,例如

    option = ["rock", "paper", "scissors"]
    computer_choose = random.choice(option)
    

    【讨论】:

      【解决方案3】:

      你必须在双引号之间到处写石头剪刀布。因为它是一个字符串。而在 python 语言中,我们使用 "" 或 '' 来表示字符串。

      这是修正后的代码 ->

      # Rock Paper and Scissors Game
      
      import random
      
      rock = "?"
      paper = "?"
      scissors = "✂"
      computer_option = ["rock" , "paper", "scissors"]
      
      computer_chooses = random.choice(computer_option)
      
      player_chooses = input("What do you choose ? ").lower()
      
      print("The computer choose :", computer_chooses)
      
      if player_chooses == "rock" and computer_chooses == "scissors":
          print("You Win")
      elif player_chooses == "paper" and computer_chooses == "rock":
          print("You Win")
      elif player_chooses == "scissors" and computer_chooses == "paper":
          print("You Win")
      elif player_chooses == computer_chooses:
          print("Draw")
      else:
          print("You Lose")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多