Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';不要让代码检测到Python空闲时轮到谁了_Python - Fatal编程技术网

Can';不要让代码检测到Python空闲时轮到谁了

Can';不要让代码检测到Python空闲时轮到谁了,python,Python,我的朋友和我正在尝试将“21”游戏编码为Python,在游戏中,你与一个随机的东西玩,你轮流向分数中添加数字(从1-4),如果轮到你的时候是21或以上,你就输了。 但我们不知道如何让它检测到21岁时轮到谁了 以下是我目前的代码: import random import time carryOn = True counter = 0 print("First to 21 or above LOSES\n \n") while carryOn == True: u

我的朋友和我正在尝试将“21”游戏编码为Python,在游戏中,你与一个随机的东西玩,你轮流向分数中添加数字(从1-4),如果轮到你的时候是21或以上,你就输了。 但我们不知道如何让它检测到21岁时轮到谁了

以下是我目前的代码:

import random
import time
carryOn = True
counter = 0

print("First to 21 or above LOSES\n \n") 

while carryOn == True:
    user = input("Please enter any number from 1 – 4")
    print("You Chose " + user)

    counter = counter + int(user)
    print("The new Total is " + str(counter) + "\n")

    user == 0

    time.sleep(3)

    computer = random.randint(1,4)
    print("The Computer Rolled a " + str(computer))

    counter = counter + int(computer)
    print("The new Total is " + str(counter) + "\n")
if counter == 21:
    carryOn == False

首先,当分数为21时,代码不会检测到,因为if语句不在while循环中,并且您正在比较carryOn和False,但没有将其设置为False。另外,if语句应该检查计数器是否大于或等于21,因为您可以跳过它(例如,分数是20,然后是24)

你们需要在每回合后检查计数器,而不是每两回合后检查一次,因为游戏可能在用户回合后结束。要结束中间的循环,可以使用中断或继续。我更喜欢继续,所以结果是这样的

import random
import time
carryOn = True
counter = 0

print("First to 21 or above LOOSES\n \n") 

while carryOn:

    user = input("Please enter any number from 1 – 4")
    print("You Chose " + user)

    counter = counter + int(user)
    print("The new Total is " + str(counter) + "\n")


    if counter >= 21:
        print('User lost')
        carryOn = False 
        continue

    computer = random.randint(1,4)
    print("The Computer Rolled a " + str(computer))

    counter = counter + int(computer)
    print("The new Total is " + str(counter) + "\n")

    if counter >= 21:
        print('Computer lost')
        carryOn = False


不相关,但无需测试
==True
==False
(请参见
)好的,您必须在每轮非迭代后测试21次。轮到用户后添加另一张支票