[Tutor] Fwd: Re: Passing arguments?

Ricardo Aráoz ricaraoz at gmail.com
Mon Oct 21 00:01:15 CEST 2013


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 <ricaraoz at gmail.com>
A: 	Jenny Allar <jennyallar at 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 ()
>



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131020/d31b0b46/attachment-0001.html>


More information about the Tutor mailing list