Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获得;名称“;未定义错误_Python_Python 3.x_Nameerror - Fatal编程技术网

Python 获得;名称“;未定义错误

Python 获得;名称“;未定义错误,python,python-3.x,nameerror,Python,Python 3.x,Nameerror,我班上有一个作业,这让我很头疼。 使用以下属性定义一个名为Car的类: 总里程表英里数 以英里/小时为单位的速度 驱动程序名称 主办方 里程表总英里数和速度应初始化为零 创建一个包含20辆独特车辆的列表,其中包含随机(或真实(指向外部站点的链接)、指向外部站点的链接)、驾驶员和赞助商姓名 你的主程序应该模拟比赛中车辆的进度。每模拟一分钟,车辆选择一个介于1和120之间的新随机速度,并使用以下方程式每分钟更新其里程表英里数: 里程表\英里=里程表\英里+速度*时间 由于速度以英里/小时为单位,所以

我班上有一个作业,这让我很头疼。 使用以下属性定义一个名为Car的类:

总里程表英里数

以英里/小时为单位的速度

驱动程序名称

主办方

里程表总英里数和速度应初始化为零

创建一个包含20辆独特车辆的列表,其中包含随机(或真实(指向外部站点的链接)、指向外部站点的链接)、驾驶员和赞助商姓名

你的主程序应该模拟比赛中车辆的进度。每模拟一分钟,车辆选择一个介于1和120之间的新随机速度,并使用以下方程式每分钟更新其里程表英里数:

里程表\英里=里程表\英里+速度*时间 由于速度以英里/小时为单位,所以时间也应以小时为单位(1分钟是小时的60分之一)

我想我对使用
def
的理解可能不正确。正如标题所说,当我运行我的程序时

回溯(最近一次呼叫最后一次): 文件“/Users/darrellanddawn/Documents/Nascar.py”,第63行,在 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()
嗨,这里有一些建议

  • 避免使用
    global
    关键字,而是使用
    return
    语句
  • 在代码顶部导入模块
  • 创建完成单个任务的函数
  • 将racers功能更改为
    \uuuu init\uuu
    方法
  • 驱动程序不能在Car类中初始化,因为它不是特定于任何汽车的

  • racers()
    仅在
    Car
    类中定义,而不是在
    main()
    方法中定义。请在问题中公布您遇到的错误,并指出哪一行给出了错误。您似乎误解了
    global
    。它只是说您希望在全局范围内修改具有该名称的变量。它不会创建全局变量。尝试在不使用全局变量的情况下实现它,这样可以解决该问题以及出现的下一个“x”问题。@mseivert是否不使用全局变量以便在多个函数中使用变量?我将尝试你的建议,但我要确保我理解。我学习python的第五周,但就示例、书籍、视频等而言,它们并没有给我们太多可以继续的东西。@GrayCygnus对错误和行进行了编辑。谢谢你指出这一点。非常感谢!我试图避免全球冲突,但教练给了我很少的帮助,这就是我所能想到的。
    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()