Python - Please include #Comments for each line with explanation Suppose you are
ID: 3805488 • Letter: P
Question
Python - Please include #Comments for each line with explanation
Suppose you are purchasing something online on the Internet. At the website, you get a 10% discount if you are a member. Additionally, you are also getting a discount of 5% on the item because its Father’s Day. Write a function that takes as input the cost of the item that you are purchasing and a Boolean variable indicating whether you are a member (or not), applies the discounts appropriately, and returns the final discounted value of the item. Note: The cost of the item need not be an integer.
Explanation / Answer
# link for code in case indentation mess up. https://goo.gl/K48FrL
# Take in cost of the product and a boolea indicating whether
# user is member and give 10% discount. Additionally it gives 5% discount
# to all users.
def applyDiscount(cost, isMember):
discount = 0
# apply fathers day dsicount of 5%
discount = (cost*5.0)/100
# is user is member then give additional 10% discount
if (isMember):
discount += (cost*10.0)/100
# comute final cost
finalCost = cost - discount
# return discounted Cost
return finalCost
# driver code
cost = 100.99
isMember = True
print "Discounted cost for a member"
print "original cost: " + str(cost) + " Discounted price: " + str(applyDiscount(cost, isMember))
isMember = False
print "Discounted cost for a non member"
print "original cost: " + str(cost) + " Discounted price: " + str(applyDiscount(cost, isMember))
Discounted cost for a member
original cost: 100.99 Discounted price: 85.8415
Discounted cost for a non member
original cost: 100.99 Discounted price: 95.9405
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.