Reputation: 67
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)
Upvotes: 2
Views: 462
Reputation: 2629
Couple issues to point out here and most of the are noted by other answers already, but missing an important one so I'll wrap it all up in one answer. First, the function...
def calculateFathersDayPrice(price, isMember):
if isMember:
print price * 0.85/100
else:
print price * 0.95/100
price * 85/100
? Regardless, the most straightforward approach is simply multiplying by 0.85 and 0.95.return some_value
.Here is a altered version of your function with those two changes.
def calculateFathersDayPrice(price, isMember):
if isMember:
return price * 0.85
else:
return price * 0.95
Finally, as noted by Andrey, you need to handle the user input and do something useful with it. You need to decide what kind of values to accept from the user and what you're going to do if they enter something bogus. After your line isMember = raw_input ("Are you a member?: ")
, you need to add a little code to turn the user's input into a True/False value in Python. Also, what will you do if they answer "Feta cheese is nasty!" as a response to your question? Is that True or False or do you ask them again for more reasonable input?
Upvotes: 1
Reputation: 3427
I tend to encourage no side-effects in functions, so you can use 'return' rather than 'print'.
def calculateFathersDayPrice(price, isMember):
if isMember:
return price * 0.85
return price * 0.95
price = float(raw_input("Please Enter Price of Item: "))
isMember = raw_input("Are you a member?: ").lower() in ['true', '1', 't', 'y', 'yes']
print 'Member Status: %s' % isMember
print 'Price: %.2f' % calculateFathersDayPrice(price, isMember)
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.
Upvotes: 1
Reputation: 12713
If you assume that the user enters yes
or no
being asked if he/she is a member, then you should evaluate his/her answer to True
or False
. Your program now prints price*0.85/100
in any case. And yes, there is no need in division by 100.
Upvotes: 0
Reputation: 1618
I don't quite understand what you're asking.
Additionally, you shouldn't be dividing by 100 again. It should be:
print price * 0.85
and print price * 0.95
respectively.
Upvotes: 1