石头剪刀布程序不工作(Python)

Rya*_*n A 2 python random return-value

问题:\n程序似乎不接受输入的整数。不会添加赢/输/平局计数,并且不会在调试模式下显示计算机选择

\n\n

程序的基本设计: \n编写一个程序,让用户在计算机上玩石头、剪子、布游戏。\n该程序应按如下方式工作。\n显示一个菜单:

\n\n

得分: 0 胜, 0 平, 0 负\n(D)ebug 显示计算机的选择\n(N)新游戏\n(Q)uit

\n\n

如果用户输入“Q”或“q”,程序将结束。“N”或“n”表示新游戏,“D”或“d”表示调试模式,其他任何内容都会导致显示错误消息。

\n\n
    \n
  1. 当游戏开始时,会生成一个 1 到 3 范围内的随机数。如果数字为 1,则计算机选择了 rock。如果数字是2,则计算机选择了纸张。如果数字是3,那么计算机选择了剪刀。(除非我们处于“D”ebug 模式,否则不要显示计算机的选择。)
  2. \n
  3. 用户输入他或她的选择 \xe2\x80\x9c1-rock\xe2\x80\x9d、\xe2\x80\x9c2-paper\xe2\x80\x9d 或 \xe2\x80\x9c3-scissors\xe2\键盘上的 x80\x9d。
  4. \n
  5. 显示计算机的选择。
  6. \n
  7. 根据以下规则选出获胜者:\n\xe2\x80\xa2 如果一个玩家选择石头,另一个玩家选择剪刀,则石头获胜。\n(石头打碎剪刀。)\n\xe2\x80 \xa2 如果一个玩家选择剪刀,另一个玩家选择布,则剪刀获胜。(剪刀剪布。)\n\xe2\x80\xa2 如果一个玩家选择布,另一个玩家选择石头,则布获胜。\n (纸包石。)\n\xe2\x80\xa2 如果双方做出相同的选择,则游戏平局。
  8. \n
  9. 您的程序将保存胜利、失败和平局的总数。
  10. \n
  11. 重新显示菜单并重复游戏循环。
  12. \n
\n\n

我的计划:

\n\n
import random\n\ndef main():\n\n    continuing = "y"\n\n    win = 0\n    lose = 0\n    draw = 0\n\n    while continuing == "y":\n        print("Score:", win,"wins,", draw, "draws,", lose,"losses")\n        print("(D)ebug to show computer\'s choice")\n        print("(N)ew game")\n        print("(Q)uit")\n\n        choice = input(" ")\n\n        if choice == "n" or choice == "N":\n            win, draw, lose = playgame(win, draw, lose)\n\n        elif choice == "d" or choice == "D":\n            win, draw, lose = playgame2(win, draw, lose)\n\n        elif choice == "q" or choice == "Q":\n            break\n\n\ndef playgame(win, draw, lose):\n\n    computer = random.randint(1,3)\n    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")\n\n    if computer == 1 and player == 2:\n        Score = "You won"\n        win += 1\n\n    elif computer == 1 and player == 3:\n        Score = "You lost"\n        lose += 1\n\n    elif computer == 2 and player == 1:\n        Score = "You lost"\n        lose += 1\n\n    elif computer == 2 and player == 3:\n        Score = "You won"\n        win += 1\n\n    elif computer == 3 and player == 1:\n        Score = "You won"\n        win += 1\n\n    elif computer == 3 and player == 2:\n        Score = "You lost"\n        lose += 1\n\n    elif computer == player:\n        Score = "Draw"\n        draw += 1\n\n    return (win, draw, lose)\n\ndef playgame2(win, draw, lose):\n\n    computer = random.randint(1, 3)\n    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")\n\n    if computer == 1 and player == 2:\n        Score = "You won"\n        print("Computer chose rock")\n        win += 1\n\n    elif computer == 1 and player == 3:\n        Score = "You lost"\n        print("Computer chose rock")\n        lose += 1\n\n    elif computer == 2 and player == 1:\n        Score = "You lost"\n        print("Computer chose paper")\n        lose += 1\n\n    elif computer == 2 and player == 3:\n        Score = "You won"\n        print("Computer chose paper")\n        win += 1\n\n    elif computer == 3 and player == 1:\n        Score = "You won"\n        print("Computer chose scissors")\n        win += 1\n\n    elif computer == 3 and player == 2:\n        Score = "You lost"\n        print("Computer chose scissors")\n        lose += 1\n\n    elif computer == player:\n        Score = "Draw"\n        print("Computer chose the same as you")\n        draw += 1\n\n    return (win, draw, lose)\n\n\nmain()           \n
Run Code Online (Sandbox Code Playgroud)\n

Stu*_*tLC 5

我不是 Pythonista,但猜测输入返回 strings,在与计算机的 int 进行比较之前,您需要转换为整数。

我还认为您在干燥代码时缺少一个技巧- 您应该能够拥有一个playgame方法,该方法需要一个额外的布尔参数debugmode,它不是直接调用 print ,而是调用间接调用,例如:

def debugPrint(debugString, debugMode)
     if debugMode
         print(debugString)
Run Code Online (Sandbox Code Playgroud)

希望这有道理吗?