【问题标题】:Rock Paper Scissors result石头剪刀布结果
【发布时间】:2017-11-22 06:14:41
【问题描述】:

我正在尝试创建一个程序,让用户选择玩石头、纸或剪刀。一旦他们选择了他们想要玩的游戏和想要玩的游戏数量,他们就会被告知他们是赢还是输。

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

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()

【问题讨论】:

  • 这些问题都是来自作业吗?
  • 请记住,如果有用的话,接受答案是有礼貌的...
  • 是的。没想到你能做到,谢谢
  • 我在网上上这门课,教授并没有真正教多少。由于没有人理解这些概念,课程已经延迟了两次
  • 好吧,至少看起来您正在尝试编写必要的代码,这意味着我们可以提供帮助。祝你好运!

标签: python-3.x


【解决方案1】:
  • 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.")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2016-01-16
    相关资源
    最近更新 更多