Python 3;岩石剪刀 - 只保持选择一个结果

时间:2015-10-14 21:41:25

标签: python-3.x

写一个小RPS游戏,我遇到了一个问题。

1)当我运行该功能来玩游戏时,它总是以“领带”的形式返回 即使我选择了一个失败的变量ex。 cpu = rock,player = scissors

我有点难过。当我遇到麻烦时,我经常潜伏在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)

1 个答案:

答案 0 :(得分:0)

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

    return cpuchoice

    return userchoice

分别