我正在尝试做一个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)
您的}(这绝对是不是OK)。这就是错误消息告诉你的。在
while
循环在第二次对miles < 500.00
求值时崩溃,因为miles
是第一次通过的浮点(这是可以的),但第二次是{更具体地说,
sort
不返回任何内容,因此miles = mile()
将miles
返回的None
赋值给cardistance.sort()
。在顺便说一句,你的
Car
类并不是真正的类。。。至少,它没有任何人希望类做的事情,而且您永远不会使用它。整个程序只作为类定义的副作用运行—换句话说,是偶然的。在您可能需要一个函数(或多个),而不是类定义。然后,稍后,您将调用该函数,理想情况下是从
main
调用该函数。比如:我建议你重读Python Tutorial's "Defining Functions" section。然后转到Python Tutorial's "Classes" chapter,并将所做的与通常创建和使用类的方式进行对比。在
相关问题 更多 >
编程相关推荐