[Tutor] Passing arguments?

Dave Angel davea at davea.name
Sun Oct 20 16:24:27 CEST 2013


On 20/10/2013 00:20, Jenny Allar wrote:

Earlier, you have managed to send your emails as text, but this time
you blew it and used html.  That's probably what makes the code
doublespaced in the newsgroup.  It also means that most of us see

> 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
If you had removed this line, the function would have served the need,
and you wouldn't need the other two.  One function called calc_weight
would do it.

>
>     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 ()
>
>    <snip>
'Weight:', weight)</p>
> <p class="MsoNormal"><br></p><p class="MsoNormal"><br></p><p class="MsoNormal">    if weight <= 10:</p><p class="MsoNormal">        shiprate = 1.5</p><p class="MsoNormal">        calc_weight_small(weight, shiprate)</p>
> <p class="MsoNormal">    elif weight >= 10 and weight <= 25:</p><p class="MsoNormal">        shiprate = 1.45</p><p class="MsoNormal">        calc_weight_medium(weight, shiprate)</p><p class="MsoNormal">    else:</p>

    <snip>

-- 
DaveA




More information about the Tutor mailing list