Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70215

Re:Learner looking for assistance

From Dave Angel <davea@davea.name>
Subject Re:Learner looking for assistance
Date 2014-04-14 08:34 -0400
Organization news.gmane.org
References <67147571-4509-4476-bc7b-4712688cf7b1@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.9246.1397478548.18130.python-list@python.org> (permalink)

Show all headers | View raw


Anthony Smith <jackie.walkabout@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Learner looking for assistance Anthony Smith <jackie.walkabout@gmail.com> - 2014-04-14 00:43 -0700
  Re: Learner looking for assistance Ben Finney <ben+python@benfinney.id.au> - 2014-04-14 18:11 +1000
  Re: Learner looking for assistance Anthony Smith <jackie.walkabout@gmail.com> - 2014-04-14 01:23 -0700
  Re:Learner looking for assistance Dave Angel <davea@davea.name> - 2014-04-14 08:34 -0400
  Re: Learner looking for assistance Anthony Smith <jackie.walkabout@gmail.com> - 2014-04-15 00:52 -0700
    Re: Learner looking for assistance Chris Angelico <rosuav@gmail.com> - 2014-04-15 18:03 +1000
  Re: Learner looking for assistance Steven D'Aprano <steve@pearwood.info> - 2014-04-15 08:29 +0000
    Re: Learner looking for assistance Anthony Smith <jackie.walkabout@gmail.com> - 2014-04-15 01:44 -0700

csiph-web