2014-11-22 55 views
-1

我想做一个蟒蛇程序,其中20辆车随机更新每分钟的速度(现在设置为第二个)。它追踪他们驾驶的距离,并且首先赢得500英里。纳斯卡Python项目

但是,我当我运行它得到这个错误:

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) 
+0

什么是错误? – 2014-11-22 01:54:10

+0

请给出回溯的**全文**,因为它会指出您发生问题的确切位置。 – MattDMo 2014-11-22 02:16:47

回答

0

它的miles < 500.00第二次评估你的while循环崩溃,因为miles是浮在第一时间通过(即OK),但None第二个(这是绝对不OK)。这是错误信息告诉你的。

更具体地说,sort不返回任何东西,所以miles = mile()分配给milescardistance.sort()返回None

顺便说一下,你的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,并将您正在做什么与典型创建和使用类的方式进行对比。