Hello everyone. I am in a programming fundamentals class which we are using python. I am working on a Lab problem and really am not 100% sure how to do it.

The Problem:

A shipping company (Fast Freight Shipping Company) wants a program that asks the user to enter the weight of a package and then display the shipping charges. (This was the easy part)

Shipping Costs:

2 pounds or less = $1.10
over 2 but not more than 6 pounds = $2.20
over 6 but not more than 10 pounds = $3.70
over 10 = 3.80

My teacher added on to this saying we need to

Next, you will enhance this lab to include the following:
a. Include the name of the shipper and recipient.
b. Print a properly formatted invoice.
c. The shipper is required to purchase insurance on their package. The insurance
rates are based on the value of the contents of the package and are as follows:
Package Value Rate
0 – 500 3.99
501 – 2000 5.99
2000 10.99
2. The printed invoice must include the name of the shipper and recipient. As well, this
invoice will display the total of the shipping charge and the insurance cost.

So here is my code

def main():
    #define shipping rates
    less_two = 1.10
    twoPlus_six = 2.20
    sixPlus_ten = 3.70
    tenPlus = 3.80
    shippingClass = 0
    insuranceClass = 0
    insurance1 = 3.99
    insurance2 = 5.99
    insurance3 = 10.99

    #Getting name of shipper and recipient
    company = str(input('Please enter the name of the shipping company: '))
    customer = str(input('Please enter the name of the customer :'))
    #get weight of the package
    weight = float(input('Enter the weight of the package: '))
    insurance = shippingRate(weight, shippingClass)
    #shipping classification
    if weight <= 2.0:
        shippingClass = less_two
    elif weight > 2.0 and weight <= 6.0:
        shippingClass = twoPlus_six
    elif weight > 6.0 and weight <= 10.0:
        shippingClass = sixPlus_ten
    elif weight > 10.0:
        shippingClass = tenPlus
    else:
        print('Weight has to be a possitive number')
    print()
    if insurance <= 500 and insurance >= 0:
        insuranceClass = insurance1
    elif insurance > 500 and insurance <= 2000:
        insuranceClass = insurance2
    else:
        insuranceClass = insurance3
    total = shippingRate + insuranceClass
    #New variables
    insurance = shippingRate(weight, shippingClass)    
    shippingRate(weight, shippingClass)
    #Display
    print('The total with insurance will be $', total, '.', sep='')
    print()
    int(input('Press ENTER to end'))
#calculating shipping cost.
def shippingRate(poundage, classification):
    shipping_rate = poundage * classification
    print('The shipping will cost $', format(shipping_rate, '.2f'), '.', sep='')

main()

I am getting this error

Traceback (most recent call last):
  File "F:\Python\ACC - Labs\Lab 3\lab3_shipping.py", line 50, in <module>
    main()
  File "F:\Python\ACC - Labs\Lab 3\lab3_shipping.py", line 31, in main
    if insurance <= 500 and insurance >= 0:
TypeError: unorderable types: NoneType() <= int()

I'm sure you can see that I do not have a complete understanding of what I am doing yet so any help or direction would be great. Also I am sorry for the sloppy code. I did the first part and now trying to do the 2nd and it is just a mess right now. After I figure it out I was going to clean it up.

Thank you for anyone who spends time to help me.

Dani AI

Generated

The TypeError happens because your function prints a value but does not return one, so insurance becomes None and None <= 500 raises that error. is right: return a number from your cost function. A couple more fixes will make this solid: (1) compute the shipping tier before using it, (2) do not reuse the function name when adding totals (you had total = shippingRate + insuranceClass which adds a function object, not a number), and (3) ask for the package value because the insurance tier is based on declared value, not weight or shipping cost.

Here is a compact structure that separates concerns and produces a simple invoice. It assumes the tiered numbers are per-pound rates; if your instructor intended flat charges, change shipping_cost to just return rate_by_weight(w).

def rate_by_weight(w):
    if w <= 2:   return 1.10
    if w <= 6:   return 2.20
    if w <= 10:  return 3.70
    return 3.80

def insurance_fee(value):
    if 0 <= value <= 500:    return 3.99
    if value <= 2000:        return 5.99
    return 10.99

def shipping_cost(w):
    return w * rate_by_weight(w)

shipper   = input("Shipper name: ")
recipient = input("Recipient name: ")
weight    = float(input("Weight (lb): "))
value     = float(input("Declared value ($): "))

ship = shipping_cost(weight)
ins  = insurance_fee(value)
total = ship + ins

print("\nInvoice")
print("Shipper: {}  Recipient: {}".format(shipper, recipient))
print("Weight: {:.2f} lb  Rate: ${:.2f}/lb  Shipping: ${:.2f}".format(weight, rate_by_weight(weight), ship))
print("Insurance: ${:.2f}  Total: ${:.2f}".format(ins, total))

Quick tips:

  • Validate inputs and reject negatives before calculating.
  • For a pause-at-end prompt, use input("Press Enter to exit") (do not wrap it in int(...)).
  • Keep variable names descriptive to avoid mixing up shipping cost, insurance fee, and totals.

Recommended Answers

All 3 Replies

you should return shipping_rate in the second function:

def shippingRate ( poundage , classification ):
    return poundage * classification

or

def shippingRate ( poundage , classification ):
    shipping_rate = poundage * classification
    return shipping_rate

Ahh thank you. I am out ATM but will try it when I get home.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.