【问题标题】:Python 3 ; Rock Paper Scissors - Just Keeps Choosing One Outcome蟒蛇3;石头剪刀布 - 只选择一个结果
【发布时间】:2016-01-13 04:13:46
【问题描述】:

编写一个小型 RPS 游戏时遇到了问题。

1) 当我运行该函数玩游戏时,它总是以“平局”的形式返回 即使我选择了一个失败的变量 ex。 cpu = 石头,玩家 = 剪刀

我对此感到有些困惑。当我遇到麻烦时,我通常会潜伏在 stackoverflow 论坛上,但我还没有遇到遇到同样问题的人(即使这里有过多的 RPS 问题)。

现在,我不是要求任何人为我编写和测试它 100%,而只是告诉我错误点,我会去解决问题。

谢谢!

def showRules():
    print("********** Rock, Paper, Scissors **********")
    print("Rules: Each player chooses either Rock, Paper, or Scissors.")
    print("       The winner is determined by the following rules:")
    print("       Scissors cuts Paper -> Scissors wins")
    print("       Paper covers Rock   -> Paper Wins")
    print("       Rock smashes Scissors -> Rock Wins")
    print("*******************************************")

def getCPUChoice():
    choices = ["rock", "paper", "scissors"]
    from random import randint
    randNum = randint(0,2)
    cpuchoice = choices[randNum]
    print(cpuchoice)
    return

def getUserChoice():
    choices = ["rock", "paper", "scissors"]
    userchoice = input('Please choose either rock, paper or scissors:').lower()
    print(userchoice)
    return

def declareWinner(user, computer):
    if user == computer:
        print("Tie!!")
    elif user == "rock" and computer == "paper":
        print("You lose!")
    elif user == "paper" and computer == "scissors":
        print("You lose!")
    elif user == "scissors" and computer == "rock":
        print("You lose!")


def playGame():
    showRules()
    computer = getCPUChoice()
    user = getUserChoice()
    declareWinner(user, computer)

【问题讨论】:

  • 在任何情况下都不会返回任何内容...因此 user = None,computer = None 也是如此。

标签: python-3.x


【解决方案1】:

在 getCPUChoice 和 getUserChoice 中,您打印的是选择而不是返回它们。将这些函数末尾的返回更改为

    return cpuchoice

    return userchoice

分别。

【讨论】:

  • 谢谢!我让它工作了。现在,只是为了让它更美观。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多