Python 3 Rock Paper Scissors游戏问题

时间:2019-01-30 21:32:31

标签: python loops

我想编写一个函数,该函数运行一轮Rock,Paper,剪刀,并返回用户是赢还是输。

import random
t = ["Rock", "Paper", "Scissors"]
computer = t[random.randint(0,2)]
player = False

while player == False:
    player = input("Rock, Paper, Scissors?")
    if player == computer:
        print("Tie!")
    elif player == "Rock":
        if computer == "Paper":
            print("You lose!")
        else:
            print("You win!")
    elif player == "Paper":
        if computer == "Scissors":
            print("You lose!")
        else:
            print("You win!")
    elif player == "Scissors":
        if computer == "Rock":
            print("You lose!")
        else:
            print("You win!")
    else:
        print("That's not a valid play. Check your spelling!")

我不知道要为单独的功能添加什么。 请帮助我。

2 个答案:

答案 0 :(得分:1)

这就是我要做的;为玩家赢得的次数和计算机赢得的次数各设一个变量。将它们声明为0,因为您想根据谁赢了就增加它们。还要跟踪已玩的游戏总数和您要玩的最大游戏数。然后,您基本上可以使用已有的while循环,将其复制并添加一些调整以获得所需的结果。这是您的函数外观的一个想法:

import random

t = ["Rock","Paper","Scissors"]

def tournament():
    playerWins = 0
    computerWins = 0
    gamesPlayed = 0
    maxGames = 5

    while gamesPlayed <= maxGames:
        if playerWins == 3 or computerWins == 3:
            if playerWins > computerWins:
                print("You won after ",gamesPlayed," games with a score of ",playerWins,"!")
            elif computerWins > playerWins:
                print("Computer won after ",gamesPlayed," games with a score of ",computerWins,"!")
            else:
                print("It was a tie after ",gamesPlayed," games!")

        player = input ("Rock, Paper, Scissors? ")
        computer = t[random.randint(0,2)]
        if player == computer:
            print("Tie! You: ",playerWins,", Computer: ",computerWins)
            gamesPlayed += 1
        elif player == "Rock":
            if computer == "Paper":
                computerWins += 1
                gamesPlayed += 1
                print("You lose! You: ",playerWins,", Computer: ",computerWins,"\n")
            else:
                playerWins += 1
                gamesPlayed += 1
                print("You win! You: ",playerWins,", Computer: ",computerWins,"\n")
        elif player == "Paper":
            if computer == "Scissors":
                computerWins += 1
                gamesPlayed += 1
                print("You lose! You: ",playerWins,", Computer: ",computerWins,"\n")
            else:
                playerWins += 1
                gamesPlayed += 1
                print("You win! You: ",playerWins,", Computer: ",computerWins,"\n")
        elif player == "Scissors":
            if computer == "Rock":
                computerWins += 1
                gamesPlayed += 1
                print("You lose! You: ",playerWins,", Computer: ",computerWins,"\n")
            else:
                playerWins += 1
                gamesPlayed += 1
                print("You win! You: ",playerWins,", Computer: ",computerWins,"\n")
        else:
            print("That's not a valid play. Check your spelling!")

    if playerWins > computerWins:
        print("You won after ",gamesPlayed," games with a score of ",playerWins,"!")
    elif computerWins > playerWins:
        print("Computer won after ",gamesPlayed," games with a score of ",computerWins,"!")
    else:
        print("It was a tie after ",gamesPlayed," games!")

tournament()

编辑
如果您还有其他类似的问题(例如学习Python,函数,递增等),请随时在Discord上加我!我一直在线,很乐意为您服务!我的标签是 Rivent#1591

(如果由于某种原因不允许外部交流,请让我知道,因为我不确定!只是试图防止人们在这里提出错误的问题!)

答案 1 :(得分:0)

也许最简单的方法是在“游戏循环”中添加一个额外的退出条件。

所以您的功能可能会变成

import random
t = ["Rock", "Paper", "Scissors"]
computer = t[random.randint(0,2)]
player = False

player_wins, computer_wins = 0, 0

while player == False && player_wins <=3 && computer_wins <=3:    # The extra exit condition
    player = input("Rock, Paper, Scissors?")
    if player == computer:
        print("Tie!")
    elif player == "Rock":
        if computer == "Paper":
            print("You lose!")
            computer_wins +=1      # Keep track of the wins
        else:
            print("You win!")
            player_wins +=1        # Keep track of the wins
        ...
        ...
        ...
    else:
        print("That's not a valid play. Check your spelling!")
   computer = t[random.randint(0,2)]    # Re-randomize computer's choice

请记住,每次进入或退出循环时都需要重新随机选择计算机,否则游戏将变得非常无聊。