如何在蟒蛇岩纸剪刀上添加计分器

时间:2016-12-03 23:08:09

标签: python python-3.x

import random

name = input("What is your name?")
print ("Welcome to Rock Paper Scissors", name, "!")

def computerThrow():
    throwOptions = ["rock","paper","scissors"]
    randomChoice = random.randint(0,2)
    return throwOptions [randomChoice]

def playerThrow():
  player = input("rock, paper, scissors?")
  return player

def compareThrows(player, computer):
  if player == computer:
    return("tie")
  elif player == "rock":
    if computer == "scissors":
      return("win")
    else:
      return("lose")
  elif player == "scissors":
    if computer == "paper":
      return("win")
    else:
      return("lose")
  elif player == "paper":
    if computer == "rock":
      return("win")
    else:
      return("lose")
  else:
      return("invalid")

def printMatchOutcome(result, player, computer):
  if result == "win":
    print(player, "beats", computer, " — You win!")
  if result == "lose":
    print (computer,"beats",player, "- You lose!")
  if result == "tie":
    print("tie match!")
  if result == "invalid":
    print ("invalid...try again")

def oneRound():
  player = playerThrow()
  print("You threw:",player)

  computer = computerThrow()
  print("Computer threw:",computer)

  compare = compareThrows(player, computer)
  printMatchOutcome(compare, player, computer)

#define counter variable
gamesPlayed = 0
playerWins = 0
computerWins = 0

userWantsToPlay = True
while userWantsToPlay:
    oneRound()
    response = input("Do you want to play again? (yes / no)")
    if response == 'yes':
      userWantsToPlay = True
    else:
      userWantsToPlay = False
print("Thanks for playing!")


def score(result):
  oneRound()
  if result == "win":
    return playerWins + 1
  elif result == "lose":
    return computerWins + 1

我正在尝试为我的游戏添加一个分数计数器,这样一旦userWantsToPlay = False,它将显示所玩游戏的数量,以及计算机和玩家的分数。我已将其作为一项功能启动但我不确定这是否正确?我该怎么做呢?

0 个答案:

没有答案