汽车类错误

时间:2014-12-18 04:45:32

标签: python

我遇到了解决这个问题的麻烦。我被给了2个单独的代码,这就是我遇到麻烦的地方。我在第38行收到的错误是" UnboundLocalError:本地变量'年'在转让前引用"我不确定我做错了什么,我是Python的新手,但非常感谢任何帮助。

class Car:
    def __init__(self, year_model, make, speed):
        self.__year_model = year_model
        self.__make = make
        self.__speed = 0

    ############# year_model################
    def setYear_model(self, year_model):
        self.__year_model = year_model

    def getYear_model(self):
        return self.__year_model

    ############# Make################
    def setMake(self, make):
        self.__make = make

    def getMake(self):
        return self.make

    ############# speed################
    def set_Speed(self, speed):
        if speed < 0:
            print("Speed cannot be negative")
        else:
            self.__speed = speed

    def get_Speed(self):
        return self.__speed

    ############# str ############
    def __str__(self):
        return "Make : " + self.__make + ", Model Year :" + \
            self.__year_model + ", speed =" + str(self.__speed)

    def accelerate(self):
        self.__speed = self.__speed +5

    def brake(self):
        self.__speed = self.__speed -5


def main():

    year=(input('car year: '))
    make=(input('car make: '))
    speed=0
    my_car = Car(year, make, speed)
    print('Current speed is', speed)

    #accelerate 5 times

    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())
    my_car.accelerate()
    print('My current speed:',my_car.get_speed())

    #brake five times
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())
    my_car.brake()
    print('My current speed after brake:',my_car.get_speed())

main()

然后连接到此代码。

import ct_CarDefinition

def main():
    # Create an instance of Car
    my_car = ct_CarDefinition.Car("2008", "Honda Accord")

    print(my_car)

main()

2 个答案:

答案 0 :(得分:3)

在您将变量设置为任何内容之前,您尝试使用变量year(以及makespeed):

my_car = Car(year, make, speed)

year=(input('car year: '))
make=(input('car make: '))
speed=0

除非您能以某种方式阅读用户的想法,否则在您向用户询问他的首选年份之前,您无法创建2008本田雅阁。据你所知,他可以要求2014款保时捷Boxster。所以,只需在之后创建Car 即可知道您想要创建什么。

此外,您尝试将三个参数传递给Car(year, make, speed),但只需要两个。{ Car始终以速度0开头,因此您不必将其传递一个。

year=(input('car year: '))
make=(input('car make: '))

my_car = Car(year, make)

有些语言称为数据流编程语言,可以让你创建&#34;不完整的值&#34;像这样,my_car会是一个含糊不清的Car,但只要你不尝试使用yearmake和{{1}填写,没有人关心它是不完整的。但是Python并不是这样一种语言,也不是你可能使用的大多数其他语言。

答案 1 :(得分:3)

试试这个:

def main():
    year=(input('car year: '))
    make=(input('car make: '))
    speed=0
    my_car = Car(year, make, speed)  #use this once you have assigned values for "year", "make", "speed"

另外,在类定义中更改以下内容:

class Car:
    def __init__(self, year_model, make, speed):        #add "speed" parameter
        self.__year_model = year_model
        self.__make = make
        self.__speed = 0 

此外,您没有在Car类中定义“加速”和“制动”方法。因此,使用my_car.accelerate()my_car.brake()会引发错误:

AttributeError: Car instance has no attribute 'accelerate'

您可以将以下方法添加到Car类:

############# accelerate ################
def accelerate(self):
    self.__speed = self.__speed +5

############# brake ################
def brake(self):
    self.__speed = self.__speed -5

当您在set_speed方法中调用get_speedmain时,也会将名称更改为这两种方法。

############# speed################
def set_speed(self, speed):
    if speed < 0:
        print("Speed cannot be negative")
    else:
        self.__speed = speed

def get_speed(self):
    return self.__speed

输出:

#car year: 1998
#car make: "Honda Accord"
#('Current speed is', 0)
#('My current speed:', 5)
#('My current speed:', 10)
#('My current speed:', 15)
#('My current speed:', 20)
#('My current speed:', 25)
#('My current speed after brake:', 20)
#('My current speed after brake:', 15)
#('My current speed after brake:', 10)
#('My current speed after brake:', 5)
#('My current speed after brake:', 0)