osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Writing output of function to a csv file


Hi,

In the code below, which is an exercise I'm doing in class inheritance, almost everything is working except outputting my results, which as the function stock_count, to a csv.
stock_count is working in the code.
And I'm able to open and close the csv called cars, but the stock_count output is not being written to the file.
Any suggestions?

There are 2 python files as the exercise is about class inheritance.
I'll paste the code from both.

cars.py

# Define a class for my car

class Car(object):
    # implement the car object.
    
    def __init__(self):
        self.__colour = ''
        self.__make = ''
        self.__mileage = 0
        self.engineSize = ''

    def getColour(self):
        return self.__colour

    def getMake(self):
        return self.__make

    def getMileage(self):
        return self.__mileage

    def setColour(self, colour):
        self.__colour = colour

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

    def setMileage(self, mileage):
        self.__mileage = mileage

    def paint(self, colour):
        self.__colour = colour
        return self.__colour

    def move(self, distance):
        self.__mileage = self.__mileage + distance
        return self.__mileage
    
    
class ElectricCar(Car):
     
    def __init__(self):
        self.__numberFuelCells = 1
         
    def getNumberFuelCells(self):
        return self.__numberFuelCells
    
    def setNumberFuelCells(self, value):
        self.__numberFuelCells
        
class PetrolCar(Car):
   
    def __init__(self):
        self.__choke = 0
    
    def __getChoke(self):
        return self.__choke
    
    def __setChoke(self, value):
        self.__choke = value
        
class DieselCar(Car):
   
    def __init__(self):
        self.__dpf = 0
    
    def __getDpf(self):
        return self.__dpf
    
    def __setDpf(self, value):
        self.__dpf = value
        
        
class HybridCar(Car):
   
    def __init__(self):
        self.__regeneration = 0
    
    def __getRegeneration(self):
        return self.__regeneration
    
    def __setRegeneration(self, value):
        self.__regeneration = value



dealership.py
import csv
from car import Car, ElectricCar, PetrolCar, DieselCar, HybridCar



class Dealership(object):

    def __init__(self):
        self.electric_cars = []
        self.petrol_cars = []
        self.diesel_cars = []
        self.hybrid_cars = []

    def create_current_stock(self):
        for i in range(6):
           self.electric_cars.append(ElectricCar())
        for i in range(20):
           self.petrol_cars.append(PetrolCar())
        for i in range(10):
           self.diesel_cars.append(DieselCar())
        for i in range(4):
           self.hybrid_cars.append(HybridCar())
        
        

    def stock_count(self):
        print('petrol cars in stock ' + str(len(self.petrol_cars)))
        print('electric cars in stock ' + str(len(self.electric_cars)))
        print('diesel cars in stock ' + str(len(self.diesel_cars)))
        print('hybrid cars in stock ' + str(len(self.hybrid_cars)))
        

    def rent(self, car_list, amount):
        if len(car_list) < amount:
            print('Not enough cars in stock')
            return
        total = 0
        while total < amount:
           car_list.pop()
           total = total + 1
           
           

    def process_rental(self):
        answer = input('would you like to rent a car? y/n')
        if answer == 'y':
            self.stock_count()
            answer = input('what type would you like? p/e/d/h')
            amount = int(input('how many would you like?'))
            if answer == 'p':
                self.rent(self.petrol_cars, amount)
            if answer == 'd':
                self.rent(self.diesel_cars, amount)
            if answer == 'h':
                self.rent(self.hybrid_cars, amount)
            else:
                self.rent(self.electric_cars, amount)
        self.stock_count()
        
        file = open("cars.csv","w")
        file.write(str(self.stock_count()))
        file.close()
        

dealership = Dealership()
dealership.create_current_stock()
proceed = 'y'
while proceed == 'y':
    dealership.process_rental()
    proceed = input('continue? y/n')