Python中的简单21游戏问题

Python中的简单21游戏问题,python,Python,我最近编写了一个简单的21的游戏(由用户对计算机玩),当用户/计算机赢得游戏时,我遇到了一些麻烦。这个程序背后的逻辑非常简单,运行良好,只是在有赢家之后(游戏结束后,打印语句简单地声明赢家),程序会询问用户是否想再次“掷”骰子。我完全理解为什么会出现这个问题,但我不确定如何在不使用中断语句的情况下修复它,因为我的教授不希望我们使用它们。他建议为我的while循环创建一个布尔标志来运行,但我似乎无法理解。有什么建议吗?下面列出了我的程序的完整代码。谢谢大家! import random GAME_

我最近编写了一个简单的21的游戏(由用户对计算机玩),当用户/计算机赢得游戏时,我遇到了一些麻烦。这个程序背后的逻辑非常简单,运行良好,只是在有赢家之后(游戏结束后,打印语句简单地声明赢家),程序会询问用户是否想再次“掷”骰子。我完全理解为什么会出现这个问题,但我不确定如何在不使用中断语句的情况下修复它,因为我的教授不希望我们使用它们。他建议为我的while循环创建一个布尔标志来运行,但我似乎无法理解。有什么建议吗?下面列出了我的程序的完整代码。谢谢大家!

import random
GAME_LIMIT = 21

def main():
    user_points = 0
    computer_points = 0
    print("Welcome to the game of 21! May the odds be ever in your favor...")
    print()
    answer = get_response()
    while answer == "y":
        points, comp_points = roll_dice()
        user_points += points
        computer_points += comp_points
        print("Points:", user_points)
        if user_points == GAME_LIMIT:
            print("User's Points:", user_points)
            print("Computer's Points:", computer_points)
            if computer_points == GAME_LIMIT:
                print("Tie Game!")
            else:
                print("User Wins!")
        if user_points > GAME_LIMIT:
            print("User's Points:", user_points)
            print("Computer's Points:", computer_points)
            if computer_points < GAME_LIMIT:
                print("Computer Wins!")
            elif computer_points == GAME_LIMIT:
                print("Computer Wins!")
            else:
                print("Tie Game!")

        answer = get_response()

    if answer == "n":
        print("User's Points:", user_points)
        print("Computer's Points:", computer_points)
        if computer_points == GAME_LIMIT:
            print("Computer Wins!")
        elif computer_points > GAME_LIMIT:
            print("User Wins!")
        elif computer_points == user_points:
            print("Tie Game!")
        elif computer_points < GAME_LIMIT:
            if user_points < computer_points:
                print("Computer Wins!")
            else:
                print("User Wins!")


def roll_dice():
    user_roll = random.randint(1,6) + random.randint(1,6)
    computer_roll = random.randint(1,6) + random.randint(1,6)
    return user_roll, computer_roll

def get_response():
    response = input("Do you want to roll? (y/n): ")
    return response

main()
随机导入
博弈极限=21
def main():
用户积分=0
计算机点数=0
打印(“欢迎来到21世纪的游戏!祝你永远好运…”)
打印()
答案=获取响应()
而答案==“y”:
点数,复合点数=掷骰子()
用户积分+=积分
计算机积分+=综合积分
打印(“点数:”,用户点数)
如果用户积分==游戏限制:
打印(“用户点:”,用户点)
打印(“计算机点数:”,计算机点数)
如果计算机积分==游戏限制:
打印(“平局游戏!”)
其他:
打印(“用户赢!”)
如果用户积分>游戏限制:
打印(“用户点:”,用户点)
打印(“计算机点数:”,计算机点数)
如果电脑积分<游戏限制:
打印(“计算机获胜!”)
elif计算机积分==游戏限制:
打印(“计算机获胜!”)
其他:
打印(“平局游戏!”)
答案=获取响应()
如果答案=“n”:
打印(“用户点:”,用户点)
打印(“计算机点数:”,计算机点数)
如果计算机积分==游戏限制:
打印(“计算机获胜!”)
elif计算机积分>游戏限制:
打印(“用户赢!”)
elif计算机积分==用户积分:
打印(“平局游戏!”)
elif计算机积分<游戏限制:
如果用户点<计算机点:
打印(“计算机获胜!”)
其他:
打印(“用户赢!”)
def掷骰子():
用户滚动=random.randint(1,6)+random.randint(1,6)
计算机滚动=random.randint(1,6)+random.randint(1,6)
返回用户卷、计算机卷
def get_响应():
响应=输入(“您想滚动吗?(y/n):”)
返回响应
main()

你想要的是比赛是否有赢家或平局 如果您输入n来扮演角色,骰子仍然会结束游戏,我已经添加了
isEnd
,并做了一些更改(注释)

随机导入
博弈极限=21
def掷骰子():
用户滚动=random.randint(1,6)+random.randint(1,6)
计算机滚动=random.randint(1,6)+random.randint(1,6)
返回用户卷、计算机卷
def get_响应():
响应=原始输入(“是否要滚动?(y/n):”)
返回响应
def main():
用户积分=0
计算机点数=0
打印(“欢迎来到21世纪的游戏!祝你永远好运…”)
打印()
isEnd=False#添加了一个是游戏结束
虽然我没有收到:
答案=获取响应()
如果(答案==“y”):#检查还有哪些掷骰子
点数,复合点数=掷骰子()
用户积分+=积分
计算机积分+=综合积分
打印(“点数:”,用户点数)
如果用户积分==游戏限制:
打印(“用户点:”,用户点)
打印(“计算机点数:”,计算机点数)
如果计算机积分==游戏限制:
打印(“平局游戏!”)
其他:
打印(“用户赢!”)
isEnd=True#当有赢家时
如果用户积分>游戏限制:
打印(“用户点:”,用户点)
打印(“计算机点数:”,计算机点数)
如果电脑积分<游戏限制:
打印(“计算机获胜!”)
elif计算机积分==游戏限制:
打印(“计算机获胜!”)
其他:
打印(“平局游戏!”)
isEnd=True#当有赢家时
如果(答案=“n”):
isEnd=True#或退出
如果名称=“\uuuuu main\uuuuuuuu”:
main()

如果可以,您应该始终为类似的内容包含示例输出。您可以在某人获胜后返回,尽管不可否认,这与您的讲师出于任何原因反对的“中断”没有任何区别。一种布尔方法是,在检查游戏是否仍在运行时,将当前的
嵌套在
循环中。创建一个名为winner set的变量,将其设置为False。当有赢家时,将其设置为真,然后将其添加到您的帐户中。此外,您不应该复制和粘贴用于打印赢家的代码。有赢家后,程序应该做什么。它应该退出还是什么?
import random
GAME_LIMIT = 21

def roll_dice():
    user_roll = random.randint(1,6) + random.randint(1,6)
    computer_roll = random.randint(1,6) + random.randint(1,6)
    return user_roll, computer_roll

def get_response():
    response = raw_input("Do you want to roll? (y/n): ")
    return response


def main():
    user_points = 0
    computer_points = 0
    print("Welcome to the game of 21! May the odds be ever in your favor...")
    print()
    isEnd = False  # added a is game end

    while not isEnd:
        answer = get_response()
        if(answer == "y"): #checkes whther still roll dice
            points, comp_points = roll_dice()
            user_points += points
            computer_points += comp_points
            print("Points:", user_points)
            if user_points == GAME_LIMIT:
                print("User's Points:", user_points)
                print("Computer's Points:", computer_points)
                if computer_points == GAME_LIMIT:
                    print("Tie Game!")
                else:
                    print("User Wins!")
                isEnd = True #When ther is winner
            if user_points > GAME_LIMIT:
                print("User's Points:", user_points)
                print("Computer's Points:", computer_points)
                if computer_points < GAME_LIMIT:
                    print("Computer Wins!")
                elif computer_points == GAME_LIMIT:
                    print("Computer Wins!")
                else:
                    print("Tie Game!")
                isEnd = True #When ther is winner

        if (answer == "n"):
            isEnd = True #or the exit





if __name__ == "__main__":
    main()