Learner looking for assistance

A

Anthony Smith

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
 
A

Anthony Smith

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
 
D

Dave Angel

Anthony Smith said:
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.
 
A

Anthony Smith

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
 
C

Chris Angelico

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
 
S

Steven D'Aprano

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?
 
A

Anthony Smith

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.








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.












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.










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.













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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top