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

2023-12-13

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

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

战绩:0胜0平0负 (调试显示计算机的选择 (新游戏 (辞职

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

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

我的计划:

import random

def main():

    continuing = "y"

    win = 0
    lose = 0
    draw = 0

    while continuing == "y":
        print("Score:", win,"wins,", draw, "draws,", lose,"losses")
        print("(D)ebug to show computer's choice")
        print("(N)ew game")
        print("(Q)uit")

        choice = input(" ")

        if choice == "n" or choice == "N":
            win, draw, lose = playgame(win, draw, lose)

        elif choice == "d" or choice == "D":
            win, draw, lose = playgame2(win, draw, lose)

        elif choice == "q" or choice == "Q":
            break


def playgame(win, draw, lose):

    computer = random.randint(1,3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        lose += 1

    elif computer == player:
        Score = "Draw"
        draw += 1

    return (win, draw, lose)

def playgame2(win, draw, lose):

    computer = random.randint(1, 3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        print("Computer chose rock")
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        print("Computer chose rock")
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        print("Computer chose paper")
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        print("Computer chose paper")
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        print("Computer chose scissors")
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        print("Computer chose scissors")
        lose += 1

    elif computer == player:
        Score = "Draw"
        print("Computer chose the same as you")
        draw += 1

    return (win, draw, lose)


main()           

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

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

def debugPrint(debugString, debugMode)
     if debugMode
         print(debugString)

希望这有道理吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

石头剪刀布程序不工作(Python) 的相关文章

随机推荐