I’m struggling trying to find out how to add the math into this function:
Question: Write a program which first defines a function with signature calculateFathersDayPrice(price, isMember). The function should return 95% of the price if the user is not a member and 85% of the price if the user is a member (i.e., as indicated by isMember). Your program should then prompt for a price and whether or not the user is a member (you can assume that the user enters “yes” or “no”), call calculateFathersDayPrice(), and then print the price returned by the function.
My code so far:
def calculateFathersDayPrice(price, isMember):
"""Prints price*0.85/100 if isMember is True, prints price*0.95/100 if isMember is
false
"""
if isMember:
print price * 0.85/100
else:
print price * 0.95/100
price = raw_input ("Please Enter Price of Item: ")
isMember = raw_input ("Are you a member?: ")
print 'price:', price, 'Member Status:', isMember
print calculateFathersDayPrice(price, isMember)
I tend to encourage no side-effects in functions, so you can use ‘return’ rather than ‘print’.
This will check if the answer is one of the supplied true-values and then calculates the price based on the isMember status. Finally, using string formatting, you get the result with the proper amount of decimals.