Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
Python 为什么会低于0_Python - Fatal编程技术网

Python 为什么会低于0

Python 为什么会低于0,python,Python,我有一个程序正在运行,这是“赛车游戏”的一部分,但是当游戏应该结束时,它不会结束,剩余的比赛长度会下降到负数 我试着说,只有当剩下的比赛长度超过0分时,我才会跑 当=为0时停止,但仍继续 import random print("This game is a race where you can select your distance of the race and then a dice is rolled") rounds = 0 while True: try:

我有一个程序正在运行,这是“赛车游戏”的一部分,但是当游戏应该结束时,它不会结束,剩余的比赛长度会下降到负数

我试着说,只有当剩下的比赛长度超过0分时,我才会跑 当=为0时停止,但仍继续

import random
print("This game is a race where you can select your distance of the race 
and then a dice is rolled")
rounds = 0
while True:
    try:
        race_length = int(input("Choose the Length You Would Like You 
        Race To Be (Between 5 and 15)"))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #if an interger isn't entered do loop above to avoid and error
        continue
    else:
        #race length succesfully found
        #finished the loop
        break
while True:
    try:
         # asking for race length
        car_number = int(input("Choose what number car you would like to 
        be between 0-5"))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #if an interger isn't entered do loop above to avoid and error
        continue
    else:
        #car_number succesfully found
        #finished the loop
        break
 cars= 
[race_length,race_length,race_length,race_length,race_length,race_length]
print(cars[0])
print(cars[1])
while race_length >0:
    chosen = int(random.randint(0,5))
    cars[chosen] = cars[chosen] - 1
    print(cars)
    rounds = rounds + 1
    if cars[1] == 0:
        print("Car Number " + str(chosen) + " Has Finished The Race")
        print(cars)
        print("The race lasted " + str(rounds) + " Rounds")
        if chosen == car_number:
            print("Congratulations, it was your car that finished 
            first!")
        elif chosen != car_number:
            print("Sorry You Did Not Win, Better Luck Next Time")
        break
我希望在其中一个列表值中完成一次'race_length=0',但它会一直延续到-,如下所示: 1号车已经完成了比赛 [0, 0, -3, 0, -1, -1] 比赛进行了41轮
很抱歉,您没有获胜,祝您下次好运。

您可以在通过环路时从比赛长度中减去一个,并添加一条消息,说明没有赛车完成比赛。

您必须检查列表中的所有元素
赛车

while cars[0]>0 and cars[1]>0 and cars[2]>0 and cars[3]>0 and cars[4]>0 and cars[5]>0 :

您可以使用
all(list)
执行类似操作,如果所有元素都给出
True
,则返回
True
。而
0
给出
False
。所以当列表中出现
0
时,它给出
False

while all(cars):
但负值也给出了
真值
,因此当列表中的某些元素将值从ie.
1
更改为
-1
并跳过
0
时,可能会出现问题。但我认为你的代码不应该有这个问题。或者您可以使用:

while all(x>0 for x in cars):

race\u length
始终大于0,因为您从未更改它,因此您的
while
循环从未停止。即使我将其更改为cars[1]对于第三种解决方案,当这不再是真的时,我是否可以使用else:print命令来声明谁完成了比赛,或者我是否可以使用相同的方法来检查谁完成了比赛。在python中,有构造
while/else
,但它与
if/else
,在这里没有用。您必须使用
if cars[0]或
来表示枚举(cars,1)中的数字和值:if value或
winner=min((值,数字)来表示枚举(cars,1)中的数字和值);打印(获胜者[1])
并给出一辆价值最小的汽车。但是当有两辆车的值最小时,它将只显示一辆。