0

This is probably a very easy fix that I am missing. In class I am working on object manipulation. I have built the following program to take two user inputs and display a speed at the end of the program. I have tweaked the program multiple times but keep getting the following error message: "local variable "car" referenced before assignment.

class Car:
    # accepts arguments for car's year model,make,speed
    def __init__(self,year,make,speed):
        self.__year_model=year
  
        self.__make=make
    
        self.__speed=speed
    
    # set mutators
    def set_year_model(self,year):
        self.__year=year
    
    def set__make(self,make):
        self.__make=make
    
    def set__speed(self,speed):
        self.__speed=0

    # get returns
    def get_year_model(self):
        return self.__year
    
    def get_make(self):
        return self.__make
    
    def get__speed(self):
        return self.__speed
    
    # movement methods

    def accelerate(self):
        self.speed +=5
    
    def brake(self):
        self.speed -=5
    
    def get_speed(self):
        return self.__speed








# This program imports car class and utilizes it to display
# speed of car to user
import Car

def main():
    year= input("Car Year: ")
    make= input("Car Make: ")

    car= car.Car(year,make,get_speed)
    
    for count in range(5):
        car.accelerate()
        print("The current speed is:  ", car.get_speed())

    for count in range(5):
        car.brake()
        print("The current speed is now:  ", car.get_speed())
main()

Current output (file names removed for privacy):

Car Year: 1976
Car Make: TOY
Traceback (most recent call last):
  File , line 18, in <module>
    main()
  File , line 9, in main
    car= car.Car(year,make,get_speed)
UnboundLocalError: local variable 'car' referenced before assignment
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • In `car.Car(...)` what did you expect that `car` refers to? – mkrieger1 Feb 18 '21 at 13:53
  • 2
    This class looks like Java, written in Python. Why all those getters and setters? Why all those attributes with two leading underscores (and no, two leading underscores don't make an attribute unaccessible from outside)? – Matthias Feb 18 '21 at 13:56
  • 2
    As a side note, you really don't want to use attribute names starting with double underscores (see https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name), and using setters and getters is really not something one does in Python (see https://stackoverflow.com/a/36943813/550094) – Thierry Lathuille Feb 18 '21 at 13:56
  • Also, note that [the PEP 8 style guide for Python](https://www.python.org/dev/peps/pep-0008/#package-and-module-names) says that "Modules should have short, all-lowercase names." - so your module should be named 'car' rather than 'Car'. – Thierry Lathuille Feb 18 '21 at 14:01
  • I've updated my code to car=Car.Car(year, make, get_speed). I now am receiving an error message saying get_speed is not defined. I then tried to change it to speed with the same response, _speed, __speed all throw the same error. If I change parameters to (year, make, 0) , error code reads: 'Car' object has not attribute speed. What am I missing? – stay_caffinated_always Feb 18 '21 at 14:39

3 Answers3

1

You had wrong module name, you import Car not car, so you must access to Car class by

car= Car.Car(year,make,get_speed)
rozumir
  • 845
  • 9
  • 20
0

It's your class instance instantiation that is misspelled. Here's a replacement for your main program and the Car module:

class Car:
    # accepts arguments for car's year model,make,speed
    def __init__(self,year,make,speed):
        self.__year_model = year
        self.__make = make
        self.__speed = speed
    
    # set mutators
    def set_year_model(self,year):
        self.__year = year
    
    # Replaced middle "dunder" with single underscore
    def set_make(self,make):
        self.__make = make

    # Replaced middle "dunder" with single underscore   
    def set_speed(self,speed):
        self.__speed = speed

    # get returns
    def get_year_model(self):
        return self.__year
    
    def get_make(self):
        return self.__make

    # Replaced middle "dunder" with single underscore
    def get_speed(self):
        return self.__speed
    
    # movement methods
    def accelerate(self):
        self.__speed += 5
    
    def brake(self):
        self.__speed -= 5

    # Removed "duplicate" get_speed() method


# This program imports car class and utilizes it to display
# speed of car to user
import Car

def main():
    year = input("Car Year: ")
    make = input("Car Make: ")

    # Replaced "get_speed" with zero
    car = Car.Car(year, make, 0)
    
    for count in range(5):
        car.accelerate()
        print("The current speed is:  ", car.get_speed())

    for count in range(5):
        car.brake()
        print("The current speed is now:  ", car.get_speed())
main()
VirtualScooter
  • 1,792
  • 3
  • 18
  • 28
  • Thanks for your help with this. I am now having this occur. I've updated my code to car=Car.Car(year, make, get_speed). I now am receiving an error message saying get_speed is not defined. I then tried to change it to speed with the same response, _speed, __speed all throw the same error. If I change parameters to (year, make, 0) , error code reads: 'Car' object has not attribute speed. What am I missing? – stay_caffinated_always Feb 18 '21 at 14:58
  • OK. I'll update the answer to show how you could fix this. – VirtualScooter Feb 18 '21 at 15:35
0

change this

car= car.Car(year,make,get_speed)

to

car= Car.Car(year,make,get_speed)
user3697625
  • 167
  • 4
  • 17