Help with some python homework...

David Hutto dwightdhutto at gmail.com
Sun Feb 2 13:09:08 EST 2014


On Sunday, February 2, 2014 12:43:01 PM UTC-5, Denis McMahon wrote:
> On Sun, 02 Feb 2014 08:57:03 -0800, David Hutto wrote:
> 
> 
> 
> > Revised:
> 
> 
> 
> > discounted_price = price_per_book - (price_per_book * percent_discount)
> 
> 
> 
> by applying some simple algebra to the right hand side
> 
> 
> 
> price_per_book - (price_per_book * percent_discount)
> 
> 
> 
> "x = (x * 1)" so "price_per_book == (price_per_book * 1)" so rhs becomes
> 
> 
> 
> (price_per_book * 1) - (price_per_book * percent_discount)
> 
> 
> 
> and "(a * x) - (a * y)" == "a * (x - y)" so rhs becomes
> 
> 
> 
> price_per_book * (1 - percent_discount)
> 
> 
> 
> hence:
> 
> 
> 
> discounted_price = price_per_book * (1 - percent_discount)
> 
> 
> 
> -- 
> 
> Denis McMahon

The one just looks out of place compare to using properly defined names,(algebra aside) like this:

def order_total():
	price_per_book = float(raw_input("Enter price per book: $"))
	percent_discount_amount = float(raw_input("Enter percent discount amount(in format example .40): "))
	quantity = float(raw_input("Enter quantity of books: "))
	first_book_shipping = float(raw_input("Enter first book's shipping: $"))
	extra_book_shipping = float(raw_input("Enter extra book's shipping costs: $"))
	percent_discount = price_per_book * percent_discount_amount
	amount_of_first_books = 1 # of course it would equal 1
	discounted_price = price_per_book - percent_discount
	shipping = first_book_shipping + ((quantity - amount_of_first_books) * extra_book_shipping) 
	total_price = (quantity * discounted_price) + shipping
	print 'Total price: $%d' % (total_price) 

order_total()



************Or Use with params for iterating through larger amounts of books to be calculated*****************




def order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping):
	percent_discount = price_per_book * percent_discount_amount
	amount_of_first_book = 1 # of course it would equal 1
	discounted_price = price_per_book - percent_discount
	shipping = first_book_shipping + ((quantity - amount_of_first_book) * extra_book_shipping) 
	total_price = (quantity * discounted_price) + shipping
	print 'Total price: $%d' % (total_price) 


price_per_book = float(raw_input("Enter price per book: $"))
percent_discount_amount = float(raw_input("Enter percent discount amount(in format example .40): "))
quantity = float(raw_input("Enter quantity of books: "))
first_book_shipping = float(raw_input("Enter first book's shipping: $"))
extra_book_shipping = float(raw_input("Enter extra book's shipping costs: $"))

order_total(price_per_book,percent_discount_amount,quantity,first_book_shipping,extra_book_shipping)






The numbers just seem out of place when a var can be used that properly defines it, or another way to arrive at the same solution.



More information about the Python-list mailing list