[Tutor] Passing arguments?
Jugurtha Hadjar
jugurtha.hadjar at gmail.com
Sun Oct 20 16:04:53 CEST 2013
Hello,
> 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.
Your code printed that in main.
> print('Product:', product)
>
> print('Weight:', weight)
Also, you have an int(input("weight:"))
Weight seldom is an integer.
There's also no need to create three functions
(calc_weight_[small|medium|large]) that do the exact same thing: i.e
multiply the ship rate by the weight.
Also, there is overlap in your calculations:
if weight<=10
shiprate = 1.5
...
elif weight >=10
shiprate = 1.45
What if weight = 10 pounds ? Is the shiprate 1.5 or 1.45 ? In other
words, you have to exclude one. (The second, to get the most money).
if weight<=10
shiprate = 1.5
elif weight>10
shiprate = 1.45
Now for the code:
Why not make one function that does things:
Warning: I'm on Python 2.7, so take into account raw_input() and print()
differences.
Also, I haven't modified the type of product_weight and let it integer.
You might want to change that. I haven't sanitized the inputs, neither.
--code starts here--
def calc_shiprate(product_name, product_weight):
# Takes two (2) arguments: "product_name" and "product_weight".
# Returns "amount_to_pay" by applying ship-rates depending on
# "product_weight".
# Following are the ship-rates in $/pound
shiprate_small = 1.5
shiprate_medium = 1.45
shiprate_large = 1.4
if product_weight <=10:
shiprate = shiprate_small
elif (product_weight > 10) and (product_weight <= 25):
shiprate = shiprate_medium
else:
shiprate = shiprate_large
amount_to_pay = product_weight * shiprate
print "Total to pay for product %s weighing %d pounds is: %d " %
(product_name, product_weight, amount_to_pay)
product_name = raw_input("Product name: ")
product_weight = int(raw_input("Product weight: "))
calc_shiprate(product_name, product_weight)
--code ends here--
--
~Jugurtha Hadjar,
More information about the Tutor
mailing list