[英]Why is it so that I keep getting the error that Driver sponsor is not defined, even though I placed it within a class?
I have been trying my hand at a NASCAR project, where I would have to use a class to create 20 unique vehicles and then have them race ( or to see who would reach 500 miles first, through the means of repeatedly choosing a different speed between 1 and 120 and adding it to an increasing odometer). 我一直在尝试参与NASCAR项目,在该项目中,我必须使用一堂课来制造20辆独特的汽车,然后让它们比赛(或者通过反复选择不同的速度,看看谁先达到500英里) 1和120,并将其添加到增加的里程表中)。 I made what you see below and ran it, and it boots well into the Python IDLE. 我制作了您在下面看到的内容并运行了它,并将其很好地引导到Python IDLE中。 However, it will always tell me that NameError: name 'Driver_sponsor' is not defined. 但是,它总是告诉我NameError:名称'Driver_sponsor'未定义。 See, I have been facing this error for a while now, and I have tried placing the Driver_sponsor list into a class, placing it into the Main def and placing the keyword self. 看,我已经遇到这个错误已有一段时间了,我尝试将Driver_sponsor列表放入一个类中,将其放入Main def中,并放入关键字self。 before it. 在它之前。 No matter what I did, I faced this error. 无论我做什么,我都会遇到此错误。 I am going to go back into my class book to see what I can do, but I am hoping that someone here can tell me what I am missing within my code, since, really, I am extremely lost. 我将回到课堂上看我能做什么,但是我希望这里的人可以告诉我代码中我所缺少的东西,因为实际上我非常迷茫。
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()
There are a few issues with your code. 您的代码存在一些问题。
First, you're getting this error because you're calling a variable that isn't set. 首先,由于正在调用未设置的变量,因此出现此错误。
But more importantly, you're trying to access the driver-sponsor dict before you've initialized an instance of Car
(which currently only happens inside the loop that iterates over Driver_sponsor
!). 但更重要的是,您要在初始化Car
实例之前尝试访问driver-sponsor dict(当前仅在迭代Driver_sponsor
的循环内发生!)。
If you want to loop over driver-sponsor pairs and initialize a new Car
for each one, then do you really need the full Driver_sponsor
dict initialized for every Car
? 如果要遍历驱动程序-赞助商对并为每个配对初始化一个新的Car
,那么您真的需要为每个Car
初始化完整的Driver_sponsor
dict吗? If so, just pass it as an argument when constructing Car
and populate self.__Driver_sponsor
. 如果是这样,则在构造Car
时将其作为参数传递并填充self.__Driver_sponsor
。
For example: 例如:
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
Second, you are only asking for key
when looping over the Driver_sponsor
dict, but you call on both key
(for Driver
) and val
(for Sponsor
) in each loop . 其次,您仅在遍历Driver_sponsor
dict时要求key
,但是在每个循环中都调用key
(对于Driver
)和val
(对于Sponsor
)。 Extract both key
and val
in your loop creation. 在循环创建中提取key
和val
。 You'll need the .items()
dict method to get both values: 您需要.items()
dict方法来获取两个值:
for key, val in driver_sponsor_pairs.items():
...
Third, your Car
__init__
expects Driver
and Sponsor
arguments, but you try to define CurrentCar = Car()
and then populate CurrentCar.Driver
and CurrentCar.Sponsor
afterwards. 第三,您的Car
__init__
期望使用Driver
和Sponsor
参数,但是您尝试定义CurrentCar = Car()
,然后在其后填充CurrentCar.Driver
和CurrentCar.Sponsor
。 Continuing with the above updates, try instead: 继续进行以上更新,请尝试:
CurrentCar = Car(Driver=key, Sponsor=val)
Fourth, you won't need the Retrieve_sponsor()
method if you already have the .Sponsor
attribute set. 第四,如果已经设置了.Sponsor
属性,则不需要Retrieve_sponsor()
方法。
There are a lot of misunderstandings here about Object syntax and design. 关于对象语法和设计,这里有很多误解。 You may find it frustrating to try and debug at this level of complexity. 您可能会发现尝试以这种复杂性级别进行调试令人沮丧。 I would suggest starting very simply, say, with Car()
having just one attribute. 我建议从Car()
仅具有一个属性开始非常简单。 Test that, make sure it works as you want, and then build more attributes and methods from there. 测试一下,确保它可以按需要工作,然后从那里构建更多的属性和方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.