简体   繁体   English

为什么这样,即使我将其放置在一个类中,也始终收到未定义Driver Sponsor的错误?

[英]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. 在循环创建中提取keyval 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__期望使用DriverSponsor参数,但是您尝试定义CurrentCar = Car() ,然后在其后填充CurrentCar.DriverCurrentCar.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.

相关问题 即使定义了错误,我仍会得到未定义的错误信息 - I keep getting the error backcard not defined, even though I have defined it "为什么我收到一个错误,指出未定义 k,即使我已返回它?" - Why am I getting an error stated that k is not defined, even though i have returned it? 为什么即使输入是整数,我也会不断收到 TypeError? - Why do I keep getting TypeError even though input is an interger? 即使一切似乎都很好,我也会得到一个定义的错误 - I am getting a defined error even though everything seems to be fine 为什么在上述类中定义TwitterClient时,为什么不断收到93行错误,提示未定义TwitterClient? - Why do I keep getting a line 93 error saying TwitterClient not defined when it is defined in a class above? 当我尝试在终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它 - I keep getting "ModuleNotFound" error when i try to run code on my terminal even though i installed it 我不断收到 name 'message' not defined in python 即使我在 function 中将其设为全局变量并且我正在调用它 - I keep getting name 'message' not defined in python even though I made it a global variable in the function and I'm calling it 我不断收到错误消息,说我的类名未在 python 中定义 - i keep getting the error saying that my class name is not defined in python 为什么即使我不访问此文件夹也会收到此错误? - Why am I getting this error even though I don't even access this folder? 为什么我不断收到名称未定义的错误? - Why do I keep getting the name not defined error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM