[Tutor] Passing arguments?

Jenny Allar jennyallar at gmail.com
Sun Oct 20 06:20:47 CEST 2013


I've written the code below the assignment, and I think I have everything
covered in terms of asking the user for the information I need and somehow
calculating costs, but I'm just ridiculously confused on the order and
placement of the functions and components of this program- specifically the
shiprate variable. Thank you in advance for any help.

This is my assignment:

Write a program that asks a user for the name of a product that they are
ordering online and its weight.  The program must calculate the cost of
shipping the product using the following cost structure.  If it weighs less
than 10 pounds the cost is $1.50 per pound, if it is 10 pounds or more and
less than 25 pounds then the cost is $1.45 per pound.  If the weight is 25
pounds or more the cost is $1.40 per pound.  You may get the data from the
user in main.  You must print the name of the product, the weight and the
cost of shipping in a separate function.



**********NOTE:  ALWAYS USE “WHILE LOOPS” To Validate********************

*
*







# This program uses an if-else statement

# It asks for the name of a product and the weight of that product.

# It then determines the shipping cost as defined by the weight.

#

# Variable          Type          Purpose

# product           string          hold for name of product

# weight             float           hold for weight of product

#


def main ():



    product = input("Please enter the name of the product: ")

    weight = int(input("Please enter the weight of the product: "))


    print('Product:', product)

    print('Weight:', weight)



    if weight <= 10:

        shiprate = 1.5

        calc_weight_small(weight, shiprate)

    elif weight >= 10 and weight <= 25:

        shiprate = 1.45

        calc_weight_medium(weight, shiprate)

    else:

        shiprate = 1.4

        calc_weight_large(weight, shiprate)


# Calculate shipping cost for product less than 10 pounds

def cacl_weight_small(weight, shiprate):

    shiprate = 1.5

    total = weight * shiprate

# Calculate shipping cost for product between 10 and 25 pounds.

def calc_weight_medium(weight, shiprate):

    shiprate = 1.45

    total = weight * shiprate

# Calculate shipping cost for product over 25 pounds.

def calc_weight_large(weight, shiprate):

    shiprate = 1.4

    total = weight * shiprate



#

#This function calculates and prints the total cost

# based on the weight category the product falls into

# Variable            Type         Purpose

#  weight               float        weight of product

#  shiprate            float        cost per pound

#  total                   float        total cost to ship product

#





    print ()

    print ("The cost to ship", product, "is:        $", format (total,
 '9,.2f'))











main ()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131020/2282b929/attachment.html>


More information about the Tutor mailing list