获取“名称”未定义错误

时间:2017-07-15 16:13:39

标签: python python-3.x nameerror

我的班级任务对我来说很头疼。 使用以下属性定义名为Car的类:

总里程里数

以每小时英里数的速度

司机名称

赞助

总的里程表里程和速度应该初始化为零。

创建一个包含20个独特车辆的列表,其中包含随机(或实际)(链接到外部站点。)链接到外部站点。)驱动程序和赞助商名称。

您的主程序应该模拟比赛中车辆的进度。每个模拟的分钟,车辆选择1到120之间的新随机速度,并使用以下等式每分钟更新它们的里程表里程:

odometer_miles = odometer_miles + speed * time 由于速度以英里/小时为单位,因此时间也应以小时为单位(1分钟为1/60小时)。

我想也许我对使用def的理解是关闭的。正如标题所说,当我运行我的程序时,它说

  

追踪(最近一次通话):     文件“/Users/darrellanddawn/Documents/Nascar.py”,第63行,in       main()的

     

文件“/Users/darrellanddawn/Documents/Nascar.py”,第57行,主要       赛手()

     

NameError:名称'racers'未定义

关于我收到此错误的原因的任何建议或解释?

start = True

#Car and drivers

class Car:
    def racers():
        global miles
        miles = 0
        speed = 0
        drivers = {'00' : 'Van Hellsing', '01' : 'Vlad Dragul', '02' :'Lightening McSeen',
'03' : 'Viktor Frankenstein', '04' : 'Richy Rich', '05' : 'Lynn Steely',
'06' : 'Roscoe Bautista','07' : 'Matt Pilling', '08' :'Fredric Montrose', 
'09' : 'Ward Clutts', '10' :'Miles Bruck', '11': 'Darrin Isakson', '12' :'Chauncey Speno', 
'13' : 'Billie Coghill', '14' : 'Donn Lusher', '15' : 'Vaughn Naugle', '16' :'Patrick Climer',
'17' : 'Jerome Harring', '18' : 'Carlo Bohanon', '19' : 'Brian Coggins'}
    sponsers = {'00' : 'Pepesi', '01' : 'Coke', '02' : 'Pensoil',
'03' : 'Wal-Mart', '04' : 'Exxon', '05' : 'Shell',
'06' : 'Food Lion','07' : 'McDonalds', '08' :'Ubisoft', 
'09' : 'Taco Bell', '10' :'Good Year', '11': 'Apple', '12' :'Microsoft', 
'13' : 'Lowes', '14' : 'Home Depot', '15' : 'Save-A-Lot', '16' :'Sprint',
'17' : 'Verison', '18' : 'Virgin Mobile', '19' : 'Huggies'}

def race():
    import random
    global speed
    winner = False
    time = 0

    while not winner:
        speed = random.randint(1,120)
        time = + 1
        miles = miles + (speed / 60) * time

def win(driver, sponser):
    if miles == 500:
        winner = True

        while winner:
            print('The winner is: ', drivers, sponsers)


def stop():
    print('Thank you for watching the UAT 500!')
    import sys
    sys.exit(0)

def main():
    Car
    racers()
    race()
    win()
    stop()

while start:
    main()

1 个答案:

答案 0 :(得分:1)

class Car():

    #initializes properties of the car
    def __init__(self,driver_name,sponsor,car_name):
        self.miles = 0 # self refers to car instance
        self.speed = 0
        self.driver_name = driver_name
        self.sponsor = sponsor
        self.car_name = car_name

    # changes the cars speed to a new speed
    def update_speed(self, new_speed):
        self.speed = new_speed

    #calculates the total distance traveled
    def distance_traveled(self, elapsed_time):
        self.miles += self.speed * elapsed_time


def race(car1,car2,distance):

    elapsed_time = 0.0
    _time = 0.01
    won = False

    while not won:

        #updates speed of the car to a random int between 100 and 150
        car1.update_speed(random.randint(100,150))
        #print("{} speed: {}".format(car1.car_name,car1.speed))
        #updates the distance traveled with increments of _time
        car1.distance_traveled(_time)
        #print("{} miles: {}".format(car1.car_name,car1.miles))

        car2.update_speed(random.randint(100,150))
        # print("{} speed: {}".format(car2.car_name,car2.speed))
        car2.distance_traveled(_time)
        # print("{} miles: {}".format(car2.car_name,car2.miles))

        if car2.miles >= distance:
            print("{} is the winner!!".format(car2.driver_name))
            won = True

        if car1.miles >= distance:
            print("{} is the winner!!".format(car1.driver_name))
            won = True

        elapsed_time += _time
        #print("elapsed time: {}".format(elapsed_time))

def main():
    #instantiates two car objects
    car1 = Car("Van Hellsing","Pepsi","car1")
    car2 = Car("Vlad Dragul","Coke","car2")

    # races the two cars
    race(car1,car2,10)

if __name__ == "__main__":
    main()

嗨,这里有一些建议......

  1. 避免使用global关键字而是使用return声明
  2. 导入代码顶部的模块
  3. 创建完成单个任务的函数
  4. 将赛车手功能更改为__init__方法
  5. 驱动程序未在Car类中初始化,因为它并非特定于任何汽车