osdir.com


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

Writing output of function to a csv file


On 2020-05-25 19:00, Ciar?n Hudson wrote:
> 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?
> 
[snip]
> 
>      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':
>              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()
>          
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.