我一直在努力参加一个NASCAR项目,在那里我将不得不用一个班级来创造20辆独特的车辆,然后让他们参加比赛(或者通过反复选择一个方式来看谁将首先达到500英里1到120之间的不同速度,并将其添加到增加的里程表)。我做了你在下面看到的并运行它,它很好地引导到Python IDLE。但是,它总是告诉我NameError:名称'Driver_sponsor'未定义。看,我现在已经面临这个错误一段时间了,我尝试将Driver_sponsor列表放入一个类中,将其放入Main def并放置关键字self。在它之前。不管我做了什么,我都遇到了这个错误。我将回到我的课本中看看我能做些什么,但我希望这里有人可以告诉我我的代码中缺少什么,因为,真的,我非常迷失。
from random import randint
import time
class Car:
def __init__(self,Driver_Name,Sponsor):
self.__Total_Odometer_Miles = 0
self.__Speed_Miles_Per_Hour = 0
self.__Driver_Name = Driver_Name
self.__Sponsor = Sponsor
self.__Driver = ('Drivers name Missing')
self.__Sponsor = ('Sponsor Missing')
self.__Driver_sponsor = {'A.J.Allmendinger:3M','Aric Almirola:Allegiant ','Trevpr Bayne:AMR ','Ryan Blaney:Camping World ','Clint Bowyer:Chevrolet ',
'Chris Buesher:Coca-Cola','Kurt Busch:Coca-light ','Kyle Busch:Credit One ','Landon Cassill:Ford','Matt DiBenedetto:FDP',
'Austin Dillon:','Ty Dillon:','Dale Earnhardt:Jacob Companies ','Chase Elliott: M & M ','Denny Hamlin: Microsoft ',
'Kevin Harvick:GoodYear ','Jimmie Johnson:Nationwide','Erik Jones:SUNOCO','Kasey Kahne:Toyota','Matt Kenseth:Visa ' }
def Name(self,Driver_Name):
self.__Driver_Name = Driver_Name
def Retrieve_Name(self):
return self.__Driver_Name
def __mutualize__(self):
self.__Total_Odometer_Miles = 0
self.__Speed_Miles_Per_Hour = 0
def sponsors(self):
self.__Driver_sponsor = {'A.J.Allmendinger:3M','Aric Almirola:Allegiant ','Trevpr Bayne:AMR ','Ryan Blaney:Camping World ','Clint Bowyer:Chevrolet ',
'Chris Buesher:Coca-Cola','Kurt Busch:Coca-light ','Kyle Busch:Credit One ','Landon Cassill:Ford','Matt DiBenedetto:FDP',
'Austin Dillon:','Ty Dillon:','Dale Earnhardt:Jacob Companies ','Chase Elliott: M & M ','Denny Hamlin: Microsoft ',
'Kevin Harvick:GoodYear ','Jimmie Johnson:Nationwide','Erik Jones:SUNOCO','Kasey Kahne:Toyota','Matt Kenseth:Visa ' }
def Retrieve_sponsor(self,Driver_sponsor):
return self.__Driver_sponsor
def main():
for key in Driver_sponsor():
CurrentCar = Car()
CurrentCar.Driver = key
CurrentCar.Sponsor = val
CurrentCar.MPH = randint(1,120)
time.sleep(.05)
time = 5
currentCar.ODT = 5
CurrentCar.ODT = CurrentCar.ODT + CurrentCar.MPH*Time
print(CurrentCar.Driver,CurrentCar.ODT)
if CurrentCar.ODT >= 500:
print('\ the winner is',key,'t\ sponsored by',val)
main()
答案 0 :(得分:0)
您的代码存在一些问题。
首先,您收到此错误是因为您正在调用未设置的变量。
但更重要的是,在您初始化Car
的实例(目前只发生内部循环)之前,您尝试访问驱动程序赞助商字典迭代Driver_sponsor
!)。
如果你想循环使用驱动程序赞助商对并为每个人初始化一个新的Car
,那么你真的需要为每个Driver_sponsor
初始化完整的Car
dict吗?如果是这样,只需在构建Car
时填充它作为参数并填充self.__Driver_sponsor
。
例如:
driver_sponsor_pairs = {'A.J.Allmendinger:3M',...,'Matt Kenseth:Visa'}
class Car:
def __init__(self, driver_sponsor):
# ...
self.driver_sponsor = driver_sponsor
CurrentCar = Car(driver_sponsor=driver_sponsor_pairs)
# now refer to CurrentCar.driver_sponsor
其次,您只需要key
循环Driver_sponsor
字典,但是您同时呼叫key
(Driver
}和val
(对于Sponsor
)在每个循环中。在循环创建中提取key
和val
。您需要使用.items()
dict方法来获取这两个值:
for key, val in driver_sponsor_pairs.items():
...
第三,您的Car
__init__
期望Driver
和Sponsor
个参数,但您尝试定义CurrentCar = Car()
,然后填充CurrentCar.Driver
和{ {1}}之后。继续上述更新,请尝试:
CurrentCar.Sponsor
第四,如果您已经设置了CurrentCar = Car(Driver=key, Sponsor=val)
属性,则无法使用Retrieve_sponsor()
方法。
这里有很多关于对象语法和设计的误解。您可能会发现在这种复杂程度下尝试调试很令人沮丧。我建议非常简单地开始,例如.Sponsor
只有一个属性。测试一下,确保它可以按你的需要工作,然后从那里构建更多的属性和方法。