Writing output of function to a csv file
On 2020-05-25 19:00, Ciar?n Hudson wrote:
> 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?
> 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 process_rental(self):
> answer = input('would you like to rent a car? y/n')
> if answer == 'y':
> 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)
> self.rent(self.electric_cars, amount)
> file = open("cars.csv","w")
In 'stock_count' you're telling it to print to the screen. Nowhere in
that function are you telling it to write to a file.