Home > database >  Variable referenced before assignement in Python
Variable referenced before assignement in Python

Time:12-18

`This is the code:

Class Car:
    """un simple intento de representar un coche."""

    def __init__(self, make, model, year):
        """Inicializa los atributos para describir un coche."""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """Devuelve un nombre descriptivo con formato ordenado."""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self):
        """Imprime una frase que muestra el kilometraje del coche."""
        print(f"Este coche tiene {self.odometer_reading} kilómetros.")

    def update_odometer(self, mileage):
        """Establece la lectura del cuentakilométros al valor dado.
           Rechaza el cambio si se intenta hacer retroceder el cuentakilómetros."""
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("no puedes retroceder el cuentakilómetros, hijueputa!")

    def increment_odometer(self, miles):
        self.odometer_reading  = miles

    def fill_gas_tank(self, amount):
        """Indica la cantida de combustible que tiene el coche"""
        self.amount = amount
        print(f"El coche tiene {self.amount} litros de gasolina.")

class Battery:
    """A simple attempt to model a battery for an electric car."""
    def __init__(self, battery_size=75):
        """Initialize the battery's attributes."""
        self.battery_size = battery_size
    def describe_battery(self):
        """Print a statement describing the battery size."""
        print(f"This car has a {self.battery_size}-kWh battery.")

    def get_range(self):
        """Imprime una frase acerca del alcance que proporciona esta batería."""
        if self.battery_size == 75:
            range = 418.43
        elif self.battery_size == 100:
            range = 506.94

        print(f"Este coche puede circular unos {range} kilómetros con una carga completa.")

class ElectricCar(Car):
    """Represent aspects of a car, specific to electric vehicles."""

    def __init__(self, make, model, year):
        """Initialize attributes of the parent class.
           Then initialize attributes specific to an electric car."""
        super().__init__(make, model, year)
        self.battery = Battery()

    def fill_gas_tank(self):
        """Los coches eléctricos no necesitan un tanque de gasolina."""
        print("Este coche no necesita un tanque de gasolina.")


#coche1 = Car('Ssang Yong', 'Rodius', 2015)
#coche1.fill_gas_tank(75)
#my_tesla.fill_gas_tank()

my_tesla = ElectricCar('tesla', 'model s', 2019)
my_tesla.battery.battery_size = 125
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

This is the result:

2019 Tesla Model S This car has a 125-kWh battery. Traceback (most recent call last): File "/home/usuario/PycharmProjects/pythonCrashCourse/capitulo_9_clases/electric_car_2.py", line 76, in <module> my_tesla.battery.get_range() File "/home/usuario/PycharmProjects/pythonCrashCourse/capitulo_9_clases/electric_car_2.py", line 52, in get_range print(f"Este coche puede circular unos {range} kilómetros con una carga completa.") UnboundLocalError: local variable 'range' referenced before assignment

Process finished with exit code 1

These are the calls for the class:

`

I don´t understand why it says "referenced before assignment", this is not like this, isn´t it?
I put the value before reference variable "range" i think:

> 
> my_tesla = ElectricCar('tesla', 'model s', 2019)
> **my_tesla.battery.battery_size = 125**
> print(my_tesla.get_descriptive_name())
> my_tesla.battery.describe_battery()
> **my_tesla.battery.get_range()**
> 

Thanks for your help!!!

CodePudding user response:

This happens because none of the conditions in the if statement is met, so range is not defined. You could add an else statement or whatever make sense for your settings to handle other possible battery sizes.

CodePudding user response:

In this function, if your self.battery_size is not 75 or 100 then the range variable never define means the range variable is not the code. But then you try to print the value range.

    def get_range(self):
        """Imprime una frase acerca del alcance que proporciona esta batería."""
        range_ = None     # or 0
        if self.battery_size == 75:
            range_ = 418.43
        elif self.battery_size == 100:
            range_ = 506.94
        if range_ is not None:
            print(f"Este coche puede circular unos {range_} kilómetros con una carga completa.")
        else:
            pass # print something you want


  • Related