纳斯卡Python项目

2024-09-29 19:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个python程序,其中20辆车随机速度每分钟更新一次(现在设为一秒)。它跟踪他们的行驶距离,第一到500英里就赢了。在

但是,当我运行它时,会出现以下错误:

Traceback (most recent call last): File "E:\Python\NASCAR.py", line 3, in <module> class Car: File "E:\Python\NASCAR.py", line 10, in Car while miles < 500.00: TypeError: unorderable types: NoneType() < float()

我不知道如何修复这个错误,所以请您帮忙。在

import time
from random import randint
class Car:
    miles = 0.00
    carnumber = 0
    #makes list of the cars, their speeds, and their distances. carspeed[1] is the same vehicle as cardistance[1] and "Jamie McMurray" under the Chip Ganassi Racing with Felix Stone team.
    carspeed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    cardistance = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    Cars = {"Alex Bowman":"BK Racing", "Jamie McMurray":"Chip Ganassi Racing with Felix Sabates", "David Ragan":"Front Row Motorsports", "Martin Truex Jr":"Furniture Row Racing", "Casey Mears":"Germain Racing", "JJ Yeley":"Go FAS Racing", "Jeff Gordon":"Hendrick Motorsports", "Timmy Hill":"Hillman-Circle Sport LLC", "Justin Allgaier":"H Scott Motorsports", "Jor Nemechek":"Identity Ventures Racing", "Kyle Busch":"Joe Gibbs Racing", "A.J Allmendinger":"Bushs Baked Beans", "Alex Bowman":"R Pepper", "Aric Almirola":"Smithfield foods", "Austin Dillon":"Dow Chemicals", "Black Koch":"MDS", "Bobby Labonte":"Pheonix Racing", "Brad Keselowski":"Miller Lite", "Brett moffitt":"Land Castle Title", "Brian Keselowski":"BK Motors"}
    while miles < 500.00:
        time.sleep(1)
        while carnumber != 19:
            carspeed[carnumber] = randint(0,120)
            print(carspeed)
            cardistance[carnumber] += carspeed[carnumber]/60
            carnumber += 1
        mile = cardistance.sort
        miles = mile()
        print (miles)

Tags: theinpy错误linecarfilewhile
1条回答
网友
1楼 · 发布于 2024-09-29 19:22:07

您的while循环在第二次对miles < 500.00求值时崩溃,因为miles是第一次通过的浮点(这是可以的),但第二次是{}(这绝对是不是OK)。这就是错误消息告诉你的。在

更具体地说,sort不返回任何内容,因此miles = mile()miles返回的None赋值给cardistance.sort()。在

顺便说一句,你的Car类并不是真正的类。。。至少,它没有任何人希望类做的事情,而且您永远不会使用它。整个程序只作为类定义的副作用运行—换句话说,是偶然的。在

您可能需要一个函数(或多个),而不是类定义。然后,稍后,您将调用该函数,理想情况下是从main调用该函数。比如:

def race():
    # As above...

# Later...
def main():
    race()
    return

if "__main__" == __name__:
    main()

我建议你重读Python Tutorial's "Defining Functions" section。然后转到Python Tutorial's "Classes" chapter,并将所做的与通常创建和使用类的方式进行对比。在

相关问题 更多 >

    热门问题