Here is the original task:
*
The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per Pound
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
Write a program that asks the user to enter the weight of a package and then displays the shipping charges. *

def main():
    shipweight=float(input('Please enter the weight of the item you wish to ship: '))
    shipping_charges(shipweight)


def shipping_charges(shipweight):
    if shipweight <= 2:
        print(shipweight,"pounds will cost you $1.10 per pound.")
        rate1=1.10
        total= rate1 * shipweight
        print("Therefore, your total shipping charge would be " total)

    elif shipweight <= 6:
        print(shipweight,"pounds will cost you $2.20 per pound.")
        rate2=2.20
        total= rate2 * shipweight
        print("Therefore, your total shipping charge would be", total)

    elif shipweight <= 10:
        print(shipweight,"pounds will cost you $3.70 per pound.")
        rate3=3.70
        total= rate3 * shipweight
        print("Therefore, your total shipping charge would be", total)

    elif shipweight > 10:
        print(shipweight,"pounds will cost you $3.80 per pound.")
        rate4=3.80
        total= rate4 * shipweight
        print("Therefore, your total shipping charge would be", total)

main()

when i try to run the code, it says invalid syntax. How do I fix it? (make sure you write it using functions only.) thanks

Recommended Answers

All 4 Replies

When python says invalid syntax, it shows a point in code near the point where there is an error

File "foo.py", line 11
    print("Therefore, your total shipping charge would be " total)
                                                                ^
SyntaxError: invalid syntax

If you look at your code at line 11, you will see the error. Conclusion: take the time to analyse python's error messages. You can also post these error messages in the forum. They are very useful for helpers.

An IDE like PyScripter would redline line 11 because of the missing comma.

You can also streamline your code further with another function to remove some of the repetition:

def main():
    shipweight = float(input('Please enter the weight of the item you wish to ship: '))
    shipping_charges(shipweight)

def shipping_charges_calc(rate, shipweight):
    print("{} pounds will cost you ${:0.2f} per pound.".format(shipweight, rate))
    total= rate * shipweight
    print("Therefore, your total shipping charge would be ${:0.2f}".format(total))

def shipping_charges(shipweight):
    if shipweight <= 2:
        rate=1.10
    elif shipweight <= 6:
        rate=2.20
    elif shipweight <= 10:
        rate=3.70
    # shipweight > 10 pounds
    else:
        rate=3.80
    shipping_charges_calc(rate, shipweight)

main()

Also read up on the function format(), commonly used with Python 2.7 and higher.

Nice coding though.

Or ... (sorry to be a little late getting back) ... you might like to see a type of 'table look up' solution approach .. that can reduce code bulk and streamline logic flow ...

# examplePairsInList.py #

def takeInFlt( msg ):
    ok = False
    flt = 0.0
    while not ok:
        try:
            flt = float( input( msg ) )
            ok = True
        except:
            print( "Only numbers valid input here ..." )
    return flt

def shippingRate( shipWeight, rates, topRate ):
    for pair in rates:
        if shipWeight <= pair[0]:
            return pair[1]
    else:
        return topRate


if __name__ == '__main__':

    # ratePairs is a list of pairs : [ maxLbsAndUnder, rate ] #
    ratePairs = [ [2, 1.10], [6, 2.20], [10, 3.70] ]  
    maxRate = 3.80

    more  =  True
    while more:    
        weight = takeInFlt( 'Please enter the shipping lbs: ' )
        shipRate = shippingRate(weight, ratePairs, maxRate)
        print( 'Your shipping rate: ${:0.2f}'.format(shipRate) )
        print( 'Your shipping charges: ${:0.2f}'.\
               format(weight*shipRate) )

        more = ( input( 'More (y/n) ? ' ).lower() != 'n' )
  11.  print("Therefore, your total shipping charge would be " , total)
  In line 11 you forget to write comma(,) before total.
  now, your program is running.
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.