Nascar Python项目

Nascar Python项目,python,time,printing,while-loop,distance,Python,Time,Printing,While Loop,Distance,我正在尝试制作一个python程序,其中20辆车以随机速度行驶,每分钟更新一次(现在设置为秒)。它跟踪他们行驶的距离,第一个到500英里的人获胜 但是,当我运行它时,会出现以下错误: 回溯(最近一次呼叫最后一次): 文件“E:\Python\NASCAR.py”,第3行,在 等级车: 文件“E:\Python\NASCAR.py”,第10行,在Car中 当英里数

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

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

回溯(最近一次呼叫最后一次):
文件“E:\Python\NASCAR.py”,第3行,在
等级车:
文件“E:\Python\NASCAR.py”,第10行,在Car中
当英里数<500.00时:
类型错误:无序类型:非类型()

我不知道如何修复此错误,因此非常感谢您的帮助

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)
导入时间
从随机导入randint
等级车:
英里=0.00
卡号=0
#列出汽车、速度和距离。carspeed[1]与cardistance[1]和“杰米·麦克默里”是同一辆车,由奇普·加纳西与费利克斯·斯通车队共同驾驶。
车速=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
CardInstance=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Cars={“Alex Bowman”:“BK赛车”、“Jamie McMurray”:“Chip Ganassi与Felix Sabates赛车”、“David Ragan”:“前排赛车”、“Martin Truex Jr”:“家具排赛车”、“Casey Mears”:“Germain赛车”、“JJ Yeley”:“Go FAS赛车”、“Jeff Gordon”:“Hendrick赛车”、“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”:“陶氏化学”、“Black Koch”:“MDS”、“Bobby Labonte”:“Pheonix Racing”、“Brad Keselowski”:“Miller Lite”、“Brett moffitt”:”土地城堡名称,“布莱恩·凯塞洛夫斯基”:“BK汽车”}
当英里数<500.00时:
时间。睡眠(1)
而卡号!=19:
carspeed[carnumber]=randint(0120)
打印(车速)
CardInstance[carnumber]+=carspeed[carnumber]/60
卡号+=1
mile=cardistance.sort
英里=英里
打印(英里)

您的
while
循环在第二次计算
英里数<500.00
时崩溃,因为
英里数
第一次是一个浮点值(这是正常的),但第二次是
这是错误消息告诉您的

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

顺便说一句,你的
Car
类并不是一个真正的类……至少,它没有做任何人期望类做的事情,你也从来没有使用过它。你的整个程序运行只是作为类定义的副作用——换句话说,是偶然的

您可能需要一个(或多个)函数,而不是类定义。然后,稍后,您将调用该函数,最好是从
main
。类似于:

def race():
    # As above...

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

if "__main__" == __name__:
    main()
我建议您重新阅读。然后转到,并将您所做的与类的典型创建和使用方式进行对比。

请提供回溯的全文,因为它将准确地指出问题发生的位置。