岩纸剪刀结果

时间:2017-11-22 06:14:41

标签: python-3.x

我正在尝试创建一个让用户选择玩摇滚,纸张或剪刀的程序。一旦他们选择了他们想要玩的东西以及他们想要玩的游戏数量,他们就会被告知他们是赢了还是输了。

我想计算他们赢或输的次数,但它只是在每轮之后打印。游戏完成后有没有办法让它打印出来?我想在最后向用户展示整体结果

from random import randint 

def main():
  games = int(input("How many games would you like to play?"))
  while games > 0:
    games -= 1
    comp = computer()
    choice = user()
    print("You played", choice, "and the computer played", comp)
    winner(comp, choice)



def computer():
  comp = randint in range (0,3)
  if comp == 0:
    comp = 'rock'
  elif comp == 1:
    comp = 'paper'
  elif comp == 2:
    comp = 'scissors'
  return comp

def user():
  choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
  if choice == 0:
    choice = 'rock'
  elif choice == 1:
    choice = 'paper'
  elif choice == 2:
    choice = 'scissors'
  else:
    print("invalid input")
  return choice


def winner(comp, choice):
  tie = 0
  win = 0 
  lose = 0
  while True:
    if choice == "rock" and comp == "rock":
      result = 'tie'
      tie += 1
      break
    elif choice == 'rock'and comp == 'scissors':
      result = "you win"
      win += 1
      break
    elif choice == 'rock' and comp == 'paper':
      result = "you lose"
      lose += 1
      break
    elif choice == 'paper' and comp == 'paper':
      result = 'tie'
      tie += 1
      break
    elif choice == 'paper' and comp == 'scissors':
      result = 'you lose'
      lose += 1
      break
    elif choice == 'paper' and comp == 'rock':
      result = 'you win'
      win =+ 1
      break
    elif choice == 'scissors' and comp == 'scissors':
      result = 'tie'
      tie += 1
      break
    elif choice == 'scissors' and comp == 'paper':
      result = 'you win'
      win += 1
      break
    elif choice == 'scissors' and comp == 'rock':
      result = 'you lose'
      lose +=1
      break
    else:
      print("error")
      break
  print(result)
  print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")


main()

1 个答案:

答案 0 :(得分:0)

  • winlosetie计数器
  • 声明全局变量
  • randint(0, 2)是获得随机整数的方法
  • 它是+=而不是=+

有几个错误。我在相应的行添加了评论# CORRECTION

from random import randint 

tie = 0
win = 0 
lose = 0

def main():
  games = int(input("How many games would you like to play?"))
  while games > 0:
    games -= 1
    comp = computer()
    choice = user()
    print("You played", choice, "and the computer played", comp)
    winner(comp, choice)



def computer():
  # CORRECTION: randint in range (0,3) always return FALSE. BELOW IS THE CORRECT WAY
  comp = randint(0, 2)
  if comp == 0:
    comp = 'rock'
  elif comp == 1:
    comp = 'paper'
  elif comp == 2:
    comp = 'scissors'
  return comp

def user():
  choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
  if choice == 0:
    choice = 'rock'
  elif choice == 1:
    choice = 'paper'
  elif choice == 2:
    choice = 'scissors'
  else:
    print("invalid input")
  return choice


def winner(comp, choice):  
  # CORRECTION: MAKE ALL THE COUNTERS GLOBAL
  global tie
  global win
  global lose 

  while True:
    if choice == "rock" and comp == "rock":
      result = 'tie'
      tie += 1
      break
    elif choice == 'rock'and comp == 'scissors':
      result = "you win"
      win += 1
      break
    elif choice == 'rock' and comp == 'paper':
      result = "you lose"
      lose += 1
      break
    elif choice == 'paper' and comp == 'paper':
      result = 'tie'
      tie += 1
      break
    elif choice == 'paper' and comp == 'scissors':
      result = 'you lose'
      lose += 1
      break
    elif choice == 'paper' and comp == 'rock':
      result = 'you win'
      # CORRECTION: ITS NOT =+ ITS +=
      # win =+ 1
      win += 1
      break
    elif choice == 'scissors' and comp == 'scissors':
      result = 'tie'
      tie += 1
      break
    elif choice == 'scissors' and comp == 'paper':
      result = 'you win'
      win += 1
      break
    elif choice == 'scissors' and comp == 'rock':
      result = 'you lose'
      lose +=1
      break
    else:
      print("error")
      break
  print(result)      


main()
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")