Can someone please help fix this program? I'm pretty new in Python. Every time I run this program it says :

Traceback (most recent call last):
File "E:/Python/delin.py", line 38, in <module>
discounted_total = regular_total - full_discount
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

def find_discount(total):
if total <120:
discount = 0
else:
if total <300:
discount = 0.03*total
else:
if total <500:
discount = 0.05*total
else:
discount = 0.07*total
return discount


#---------------------------------------------------------------------------

#main program

beef = 8.49
pork = 6.12
chicken = 5.21

quantity_beef= input("How many pounds of beef were bought?")

quantity_pork = input("How many pounds of pork were bought")

quantity_chicken = input("How many pounds of chicken were bought?")

total_beef = beef * quantity_beef
total_pork = pork * quantity_pork
total_chicken = chicken * quantity_chicken

regular_total = total_beef + total_pork + total_chicken

full_discount = find_discount(regular_total)

discounted_total = regular_total - full_discount

print "The total is " + discounted_total

This is Python 2.7 btw.

Edit: also, the function on the top [find_discount(total)] has the normal indentations needed. But when posting this, the lines didn't indent

Edited 6 Years Ago by hi102: Version of Python, Note

def find_discount(total):
    if total <120:
        discount = 0
    else:
        if total <300:
            discount = 0.03*total
        else:
            if total <500:
                discount = 0.05*total
            else:
                discount = 0.07*total
                return discount

            
#---------------------------------------------------------------------------

#main program

beef = 8.49
pork = 6.12
chicken = 5.21

quantity_beef= input("How many pounds of beef were bought?")

quantity_pork = input("How many pounds of pork were bought")

quantity_chicken = input("How many pounds of chicken were bought?")

total_beef = beef * quantity_beef
total_pork = pork * quantity_pork
total_chicken = chicken * quantity_chicken

regular_total = total_beef + total_pork + total_chicken

full_discount = find_discount(regular_total)

discounted_total = regular_total - full_discount

print "The total is " + discounted_total

That means no value was returned. Look at your find_discount() function. You've set what discount should be on all occasions, but you only return on one occasion! You need to move it back so it returns after all the if and else statements.

Here's how it should be.

def find_discount(total):
    if total <120:     #Check for this
        discount = 0   #It's True! Skip the else: statement.

    else:              #Above is not true, execute this block of code
        if total <300: #You get the picture..
            discount = 0.03*total
        else:
            if total <500:
                discount = 0.05*total
            else:
                discount = 0.07*total

    #All checks are done, so return
    return discount

Also, line 39 needs to be: print "The total is " + str(discounted_total) This makes it a string. You must do this because your discounted total is a float, and you can't add letters and numbers.

or you can do this

print ("The total is %f"%( discounted_total))

I prefer this ....

Edited 6 Years Ago by richieking: n/a

That means no value was returned. Look at your find_discount() function. You've set what discount should be on all occasions, but you only return on one occasion! You need to move it back so it returns after all the if and else statements.

Here's how it should be.

def find_discount(total):
    if total <120:     #Check for this
        discount = 0   #It's True! Skip the else: statement.

    else:              #Above is not true, execute this block of code
        if total <300: #You get the picture..
            discount = 0.03*total
        else:
            if total <500:
                discount = 0.05*total
            else:
                discount = 0.07*total

    #All checks are done, so return
    return discount

Also, line 39 needs to be: print "The total is " + str(discounted_total) This makes it a string. You must do this because your discounted total is a float, and you can't add letters and numbers.

ohh yay, i get it now. thanks :)

This question has already been answered. Start a new discussion instead.