首页 > 解决方案 > 二十一点程序 CPU 一直在处理。(Python)

问题描述

我最近在学校开始使用 python (3.7),我最近的任务是创建一个简单的二十一点游戏。我几乎完成了,但有一个问题,在用户 Twists 或 Sticks 之后,它直接进入 Cpu,他们只是继续发牌。我的扭曲功能运行良好,程序让您在选择坚持后选择 A 是 1 还是 11,但在显示轮到用户后,程序不断重复“庄家的第二张牌是……”经销商的总数是......”在随机停止之前“经销商一直坚持......”我确信有人可以帮助我解决一些简单的问题,问题可能是由于一些基本的误解while 循环是如何工作的,所以任何帮助表示赞赏。对不起,任何混乱的代码,

在此先感谢,代码如下

#Allows user to Twist or Stick     
while userStick == False:
    userChoice = input("Twist (T) or Stick (S)?")
    if userChoice == ("T"):
        userNumThree = random.randint(1,13)
        if userNumThree == 11 or userNumThree ==12 or userNumThree == 13:
            userNumThree = 10
        userTotal = userTotal + userNumThree
        print ("Your next card is...")
        time.sleep(0.5)
        print (userNumThree)
        if userNumThree == 1 and userAce == False:
            userAce = True
            time.sleep(0.5)
        print ("Your total is...")
        time.sleep(0.5)
        print (userTotal)
        if userTotal > 21:
            userBust = True
            print ("You have bust!")
            break

    if userChoice == ("S"):
        userStick = True
        if userAce == True:
            aceNumber = input("Do you want your ace to be a 1 or and 11")
            if aceNumber == "11":
                userTotal = userTotal + 10
            elif aceNumber == "1":
                userTotal == userTotal
            else:
                print ("I did not understand that number, so one has been chosen as default")
        print ("Okay, your final total is...")
        print (userTotal)
    break


#Allows the dealer to twist or stick            
while cpuStick == False:
    cpuTotal = cpuNumTwo
    if cpuTotal < 17:
        cpuNumThree = random.randint(1,13)
        if cpuNumThree == 11 or cpuNumThree == 12 or userNumThree == 13:
            cpuNumThree = 10
        if cpuNumThree == 1 and cpuAce == False:
            cpuAce = True
        cpuTotal = cpuTotal + cpuNumThree
        if cpuAce == True and cpuTotal + 10  >= 17 and cpuTotal + 10 <= 21:
            cpuStick = True
            cpuTotal = cpuTotal + 10
            print ("The Cpu has stuck with...")
            print (cpuTotal)                
            break

        print ("The dealer's second card is...")
        print (cpuNumThree)
        print ("The dealer's total is...")
        time.sleep(0.5)
        print (cpuTotal)

    elif cpuTotal >= 17:
        cpuStick = True
        print("The cpu has stuck with...")
        print (cpuTotal)
        break


    if cpuTotal > 21:
        cpuBust = True
        print("The dealer has Bust! You win")
        break

if userStick == True and cpuStick == True:
    if userStick > cpuStick:
        print ("You win!")
    elif cpuStick > userStick:
        print ("You lose!")

标签: pythonpython-3.xwhile-loop

解决方案


推荐阅读