Nope, it should be as a result when the conditional checks out. Yet, I do wonder how to express this “3. When apply_discount is called with a price (first argument) that is not a number (int or float) it should return The price should be a number.” How do add the function + the isinstance + conditional in a line of code?
For your first question. Well the main idea but the final example with the myObj example has not yet landed.
def apply_discount ( price, discount):
ver_price = isinstance(price, (int, float))
ver_discount = isinstance(price, (int, float))
if ver_price != True:
print (“The price should be a number”)
if ver_discount != True:
print(“The discount should be a number”)
else:
pass
calc_discount = price * (1-discount/100)
return calc_discount
print("The discount is ", apply_discount(100, 50))
you may be focusing on the wrong thing, what does it mean to return a value? it’s that what the user story or test is asking. you are using print, aren’t you?
Good job with the isinstance syntax for multiple types!
Remember that not negates an expression, so you can do:
if not (some expression):
where some expression is the value you assigned to ver_price or ver_discount.
And ILM made a good point. You need to return if a validation test fails; otherwise, the remaining code will continue to be processed…not what you want.
Not sure about this, since I used print for everything. Did not know that return could be used so extensively. I thought print had a similar function as return.
Kept trying and I do not understand why the “if price” is not working yet.
5. When apply_discount is called with a price lower than or equal to 0, it should return The price should be greater than 0.
also how can I do a check between a span of numbers chuch as 0 - 100?
6. When apply_discount is called with a discount lower than 0 or greater than 100, it should return The discount should be between 0 and 100.
def apply_discount ( price, discount):
if not isinstance(price, (int, float)):
return'The price should be a number'
if not isinstance(discount, (int, float)):
return'The discount should be a number'
if price <= 0:
return'the price should be greater than 0'
return price * (1-discount/100)
apply_discount(100, 20)
Nope nothing, and what do you mean by if it returns exactly what it says?
Right now it goes through conditionals checking data types for price and discount. Also if all of this it calculates the price and discount in the expressed way. Not fully sure how you mean or what keyword should be used or where to read it
In the screenshot you posted, what does the error message say you should return if price is lower than or equal 0? Is that exactly what you are returning? Look closely. Everything must match exactly, including capitalization.
And to test your function so you can see what it is returning, please wrap your function call in print().
Yet now I am wondering more about point 5 and 6
Failed:5. When apply_discount is called with a price lower than or equal to 0, it should return The price should be greater than 0.
When apply_discount is called with a discount lower than 0 or greater than 100, it should return The discount should be between 0 and 100.
The strings were finally copied directly as they were spelled to the 100 %
Thank you all for the help - this is so far a great learning process. Very grateful for all of the patience. This is all very new to me with the level of correctness in Python..
I feel very blessed to be able to write in this forum while learning coding and a sense of modesty