Learner looking for assistance

Dave Angel davea at davea.name
Mon Apr 14 08:34:33 EDT 2014


Anthony Smith <jackie.walkabout at gmail.com> Wrote in message:
> Hi All
> 
> I am probably doing something wrong but don't know what 
> Any help would great 
> 

As Ben pointed out,  you should be more careful with your
 copy/paste,  and especially with your indentation.  I'll assume
 these are all methods of a single class SaleNote,  and that none
 are nested.  If that's the case,  your problem is most likely
 that the second definition of save hides the first.


> Code below 
> 
> the calc_total does not return a estimated_total_weight

Right,  it returns amount.  If it gets called,  which it doesn't
 from your code here. And it doesn't save that value, it only gets
 saved by the dead code below in the first save method.
 

> 
> if add the estimated_total_weight the rest of the code works 
> 
> I am at a lose as to why ?????
> 
> def calc_total(self):
>     	amount = 0
>     	if self.estimated_weight_hd > 0:
>             amount = self.number * self.estimated_weight_hd
> 	return amount
> 		   	
> 	def save(self):
> 		self.estimated_total_weight = self.calc_total()
>     		super(SaleNote, self).save()
> 		
>     def calc_total_price(self):
>     	amount_price = 0
> 	if self.sale_head > 0:
> 	    amount_price = self.number * self.sale_head
>             return amount_price
>         else:
>             if self.estimated_total_weight > 0:
>                 amount_price = self.estimated_total_weight * self.sale_kg
> 	return amount_price
> 
>     def save(self):
> 		self.total_price = self.calc_total_price()
> 		super(SaleNote, self).save()
 
> 

To make the rest of the code more readable,  consider using else
 clauses on every if, so that you can more readily spot missing
 cases. Turns out that wasn't your problem,  but it costs every
 reader of your code the time to decide that. 

Personally,  I'd be using the max function,  which would simplify
 the first function to one line. 

-- 
DaveA




More information about the Python-list mailing list