- Did you notice your assignment specifically asks for a "while" loop for validation? That means you are expected to validate input. Run your program and when the program asks "Please enter the weight of the product in pounds: " answer "123...this is a mistake", what does it happen? Your program should answer "That is not a valid weight" and ask for the weight again. Does it? (tip: you should be using "while" and "on error")

- You have a function named cal_shiprate(), according to it's name this function should calculate the shipping rate NO MORE. But your function also prints not only the shipping rate but some other info too. So you either name your function cal_shiprate_and_print_with_prodname_and_weightf() or your function should ONLY calculate the shipping rate and you should have ANOTHER function to print the info. The recommended approach would be the second one.


El 21/10/13 23:23, Jenny Allar escribió:
Thank you all for your help with this program. Each of your answers helped me piece together an understanding of the assignment. After several days and clearly over-thinking the entire process, I ended up using the following code:

def main ():
    name = input("Please enter the name of the product: ")
weight = float(input("Please enter the weight of the product in pounds: "))

    cal_shiprate (name, weight)

def cal_shiprate(product_name, product_weight):

    if product_weight < 10:
       ship_rate = 1.5
    elif product_weight >= 10 and product_weight < 25:
       ship_rate = 1.45
    else:
        ship_rate = 1.4

    total = product_weight * ship_rate

    print("")
print(product_name, "weight in pounds ", format (product_weight, '9,.2f')) print ("Your total to ship", product_name, "is: $", format (total, '9,.2f'))


main ()




On Sun, Oct 20, 2013 at 6:01 PM, Ricardo Aráoz <ricar...@gmail.com <mailto:ricar...@gmail.com>> wrote:

    Sorry, I sent this answer to the OP directly and not to the list.

    -------- Mensaje original --------
    Asunto:     Re: [Tutor] Passing arguments?
    Fecha:      Sun, 20 Oct 2013 11:25:52 -0300
    De:         Ricardo Aráoz <ricar...@gmail.com> <mailto:ricar...@gmail.com>
    A:  Jenny Allar <jennyal...@gmail.com> <mailto:jennyal...@gmail.com>



    El 20/10/13 01:20, Jenny Allar escribió:
    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.


    Ok, just a couple of things first.

    - Your assignment specifically says 'always use "while loops" to
    validate' but I see no loop at all in your code. You might want to
    look into it.

        - What your assignment points to when it says to use "while
    loops" is that you should validate that what the user inputs is a
    valid number. If the user will input something else than a number
    as the weight then when you apply the int() function an exception
    will be flagged and your program will terminate ungracefully. You
    may validate input using "exceptions" and "while loops" (look it up).

    - Your functions should do what their name imply, and preferably
    do only one thing. Having a function named calc_weight_large()
    that also prints the shipping cost is not good.

    - Your if asks for >= 10 and your elif for <= 10 when the "= 10"
    case was already handled by the first if, that is a mistake.
    Besides which it might be better looking (though this is a matter
    of personal preference) if you ::
            if weight > 25
                # do something
            elif weight > 10
                # do something else
            else
                # do something entirely different

    - I would structure the program in the following way ::
            def main():
                product = in_product()
                weight = in_weight()

                if weight > 25
                    total = calc_shipping(1.40, weight)
                elif weight > 10
                    total = calc_shipping(1.45, weight)
                else
                    total = calc_shipping(1.50, weight)
                print_data(product, weight, total)

    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.
    #
    #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
    #
    def calc_weight_large(weight, shiprate):
        shiprate = 1.4
        total = weight * shiprate
        print ()
        print ("The cost to ship", product, "is:      $", format
    (total,  '9,.2f'))

    main ()



_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to