每当我尝试运行下面的代码时,我都会得到错误:
Traceback (most recent call last):
File "C:\Users\Admin\Downloads\NASCAR (1).py", line 29, in <module>
DriverList.Race()
AttributeError: 'list' object has no attribute 'Race'所以我显然把我的清单搞乱了。这个程序应该模拟一场比赛,然后在一辆车跑了500英里后,它会打印出获胜者的名字和赞助商,这就是我遇到问题的地方。如何修复此属性错误?
from random import randint
class Car:
def __init__(self, DriverName, SponsorName):
RaceWinner = False
self.OdometerTotal = 0
self.Time = 0
self.Driver = DriverName
self.Sponsor = SponsorName
def Race(self):
LastSpeed = 0
while self.OdometerTotal < 500:
self.Time += 1
self.CurrentSpeed = randint(1, 120)
self.OdometerTotal +=(self.CurrentSpeed / 60 * self.Time) + ((self.CurrentSpeed - LastSpeed) / (60 * self.Time))
LastSpeed = self.CurrentSpeed
if self.OdometerTotal >= 500:
print(self.Driver, "sponsored by", self.Sponsor, "was the winner.")
RaceWinner = True
return RaceWinner
def Winner(self):
if RaceWinner == True:
print(self.Name)
print(self.Sponsor)
DriverList = [Car("Aric Almirola","Richard Petty Racing"),Car("Marcos Ambrose", "Richard Petty Motorsports"),Car("Greg Biffle", "Roush Fenway Racing"),Car("Dave Blaney", "Tommy Baldwin Racing"),Car("Clint Bowyer", "Michael Waltrip Racing"),Car("Jeff Burton", "Richard Childress Racing"),Car("Kurt Thomas Busch", "Furniture Row Racing"),Car("Kyle Busch", "Joe Gibbs Racing"),Car("Liz LeMuiex", "Hendrick Motorsports"),Car("Dale Earnhardt Jr.", "Hendrick Motorsports"),Car("Carl Edwards", "Roush Fenway Racing"),Car("David Gilliland", "Front Row Motorsports"),Car("Jeff Gordon", "Hendrick Motorsports"),Car("Denny Hamlin", "Joe Gibbs Racing"),Car("Kevin Harvick", "Richard Childress Racing"),Car("Sam Hornish Jr", "Penske Racing"),Car("Jimmie Johnson", "Hendrick Motorsports"),Car("Kasey Kahne", "Hendrick Motorsports"),Car("Matt Kenseth", "Roush Fenway Racing"),Car("Brad Keselowski", "Penske Racing")]
while True:
DriverList.Race()
if RaceWinner in DriverList:
print("The winner is", DriverList.Winner())
break
DriverList = ["Aric Almirola", "Marcos Ambrose", "Greg Biffle", "Dave Blaney", "Clint Bowyer", "Jeff Burton", "Kurt Thomas Busch", "Kyle Busch", "Liz LeMuiex", "Dale Earnhardt Jr.", "Carl Edwards","David Gilliland", "Jeff Gordon", "Denny Hamlin", "Kevin Harvick", "Sam Hornish Jr", "Jimmie Johnson", "Kasey Kahne", "Matt Kenseth", "Brad Keselowski"]
SponsorList = ["Richard Petty Racing", "Richard Petty Racing", "Roush Fenway Racing", "Tommy Baldwin Racing", "Michael Waltrip Racing", "Richard Childress Racing", "Furniture Row Racing", "Joe Gibbs Racing", "Hendrick Motorsports", "Hendrick Motorsports", "Roush Fenway Racing", "Front Row Motorsports","Hendrick Motorsports", "Joe Gibbs Racing", "Richard Childress Racing", "Penske Racing", "Hendrick Motorsports", "Hendrick Motorsports", "Roush Fenway Racing", "Penske Racing"]发布于 2014-11-24 14:04:05
DriverList是一个对象列表。您需要从该列表中选择要调用函数race的对象。
#Code--------
num = 0
while num<(len(DriverList)-1):
DriverList[num].Race()
num+=1
if RaceWinner in DriverList:
print("The winner is", DriverList.Winner())
breakhttps://stackoverflow.com/questions/27098655
复制相似问题