Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70207 > unrolled thread
| Started by | Anthony Smith <jackie.walkabout@gmail.com> |
|---|---|
| First post | 2014-04-14 00:43 -0700 |
| Last post | 2014-04-15 01:44 -0700 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Anthony Smith <jackie.walkabout@gmail.com> |
|---|---|
| Date | 2014-04-14 00:43 -0700 |
| Subject | Learner looking for assistance |
| Message-ID | <67147571-4509-4476-bc7b-4712688cf7b1@googlegroups.com> |
Hi All
I am probably doing something wrong but don't know what
Any help would great
Code below
the calc_total does not return a estimated_total_weight
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()
thanks
anthony
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-04-14 18:11 +1000 |
| Message-ID | <mailman.9240.1397463097.18130.python-list@python.org> |
| In reply to | #70207 |
Anthony Smith <jackie.walkabout@gmail.com> writes:
> the calc_total does not return a estimated_total_weight
>
> if add the estimated_total_weight the rest of the code works
>
> I am at a lose as to why ?????
When it's too confusing, simplify. The code you present is fine, but is
more complex than it needs to be for the specific problem.
Also, it's not complete: it appears to be part of some larger program
that we don't have. And, it appears to have suffered re-formatting when
you put it in your message, so it won't run. (Post only in plain text,
not HTML; and copy-paste the code in plain text.)
So: start simple, make something you understand, then gradually build it
up until the point where it exhibits the behaviour you're confused by.
Then, show that simple-as-possible-but-no-simpler version here, complete
with the input you're using and the resulting output.
<URL:http://www.sscce.org/>
You may find that this process gives you enough understanding that you
are then able to solve the problem. Great! But, even if not, we'll need
you to go through that process so we can understand. Feel free to post
the simple version here if it still leaves you confused.
--
\ “Nothing worth saying is inoffensive to everyone. Nothing worth |
`\ saying will fail to make you enemies. And nothing worth saying |
_o__) will not produce a confrontation.” —Johann Hari, 2011 |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Anthony Smith <jackie.walkabout@gmail.com> |
|---|---|
| Date | 2014-04-14 01:23 -0700 |
| Message-ID | <f576c9a0-11b7-4a10-8c89-adba8597cc06@googlegroups.com> |
| In reply to | #70207 |
On Monday, 14 April 2014 17:43:41 UTC+10, Anthony Smith wrote: > Hi All > > > > I am probably doing something wrong but don't know what > > Any help would great > > > > Code below > > > > the calc_total does not return a estimated_total_weight > > > > 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() > > > > thanks > > > > anthony Thanks Ben this involves django as well forgot to mention in ear lier post Maybe it a django issue rather code. Thats why I posted it here first. thanks cheers
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-04-14 08:34 -0400 |
| Message-ID | <mailman.9246.1397478548.18130.python-list@python.org> |
| In reply to | #70207 |
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
[toc] | [prev] | [next] | [standalone]
| From | Anthony Smith <jackie.walkabout@gmail.com> |
|---|---|
| Date | 2014-04-15 00:52 -0700 |
| Message-ID | <0bbfa6d3-fe85-48e8-a26b-1f7cd3bcf822@googlegroups.com> |
| In reply to | #70207 |
On Monday, 14 April 2014 17:43:41 UTC+10, Anthony Smith wrote: > Hi All > > > > I am probably doing something wrong but don't know what > > Any help would great > > > > Code below > > > > the calc_total does not return a estimated_total_weight > > > > 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() > > > > thanks > > > > anthony Hi To see what I am trying go to yambuk.no-ip.org:8000/admin this will allow hands on what I am what the program does username python password test
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-04-15 18:03 +1000 |
| Message-ID | <mailman.9271.1397549042.18130.python-list@python.org> |
| In reply to | #70252 |
On Tue, Apr 15, 2014 at 5:52 PM, Anthony Smith <jackie.walkabout@gmail.com> wrote: > To see what I am trying go to yambuk.no-ip.org:8000/admin > > this will allow hands on what I am what the program does > Recommendation: Take the code out of this framework and just run the scripts manually. From there, you should have an easier job of figuring out what's happening. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2014-04-15 08:29 +0000 |
| Message-ID | <534cede7$0$11109$c3e8da3@news.astraweb.com> |
| In reply to | #70207 |
On Mon, 14 Apr 2014 00:43:41 -0700, Anthony Smith wrote: > the calc_total does not return a estimated_total_weight That's because you don't tell it to. Your calc_total method multiplies by estimated_weight_hd which is not the same as estimated_total_weight. > > if add the estimated_total_weight the rest of the code works > I am at a lose as to why ????? What does "works" mean in this context? What is the code supposed to do, and what does it do instead? Because you have only given us a tiny snippet of code, we have no way of running it. Please read this page here: http://www.sscce.org/ and try to prepare a short, working (in the sense that it runs and demonstrates the problem) example. > def calc_total(self): > amount = 0 > if self.estimated_weight_hd > 0: > amount = self.number * self.estimated_weight_hd > return amount This only adds the weight to amount if it is positive. If it is negative, nothing happens. Is that deliberate? Also, I see you have mixed spaces and tabs. Please use one, or the other, but never use both. Mixing spaces and tabs will give you no end of headaches. > def save(self): > self.estimated_total_weight = self.calc_total() > super(SaleNote, self).save() This appears to be indented *inside* the calc_total method, but after the return statement, it is dead code and will never be executed. Or perhaps not -- because you have mixed spaces and tabs, it is very difficult to tell. > 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 Without knowing what your code is supposed to do, we cannot tell how it is broken. What is self.sale_head? Under what circumstances is is negative? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Anthony Smith <jackie.walkabout@gmail.com> |
|---|---|
| Date | 2014-04-15 01:44 -0700 |
| Message-ID | <4e425a36-4d63-436d-9083-ac479331bf92@googlegroups.com> |
| In reply to | #70258 |
On Tuesday, 15 April 2014 18:29:27 UTC+10, Steven D'Aprano wrote: > On Mon, 14 Apr 2014 00:43:41 -0700, Anthony Smith wrote: > > > > > > > the calc_total does not return a estimated_total_weight > > > > That's because you don't tell it to. Your calc_total method multiplies by > > estimated_weight_hd which is not the same as estimated_total_weight. > > > > > > > > if add the estimated_total_weight the rest of the code works > > > I am at a lose as to why ????? > > > > What does "works" mean in this context? What is the code supposed to do, > > and what does it do instead? > > > > Because you have only given us a tiny snippet of code, we have no way of > > running it. Please read this page here: > > > > http://www.sscce.org/ > > > > > > and try to prepare a short, working (in the sense that it runs and > > demonstrates the problem) example. > > > > > > > def calc_total(self): > > > amount = 0 > > > if self.estimated_weight_hd > 0: > > > amount = self.number * self.estimated_weight_hd > > > return amount > > > > This only adds the weight to amount if it is positive. If it is negative, > > nothing happens. Is that deliberate? Also, I see you have mixed spaces > > and tabs. Please use one, or the other, but never use both. Mixing spaces > > and tabs will give you no end of headaches. > > > > > > > def save(self): > > > self.estimated_total_weight = self.calc_total() > > > super(SaleNote, self).save() > > > > This appears to be indented *inside* the calc_total method, but after the > > return statement, it is dead code and will never be executed. Or perhaps > > not -- because you have mixed spaces and tabs, it is very difficult to > > tell. > > > > > > > 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 > > > > Without knowing what your code is supposed to do, we cannot tell how it > > is broken. What is self.sale_head? Under what circumstances is is > > negative? > > > > > > > > -- > > Steven Sale head is a positive if sale head is used the first function is not required but if it is not used the first function is used then the last function is used to calculate the total price
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web