I am confused why my code only pass until third test i.e., the function should return ‘The price should be a number’ and not any other test while it doesn’t show any syntax error?
can you please post your code formatted and a link to the challenge?
we can’t debug a screenshot
When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add the backticks.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Welcome to the forum @namansekhon !
Try using isinstance syntax to check for multiple types:
isinstance(parameter, (type1, type2))
Edit:
While the use of not isinstance(price, int) could be used, it will never work the way you have written it because each side of the or operator will be evaluated separately:
(not isinstance(price, int)) or (not isinstance(price, float))
How can price be both int and float? One of the expressions will always evaluate to False when price is a number.
Happy coding!
Thank you for explaining the right way to post.
I have also corrected the isinstance() code but still none of my tests go through. Here is how the code looks now:
def apply_discount(price,discount):
if isinstance(price,(int,float)):
return('The price should be a number')
elif isinstance(discount, (int,float)):
return('The discount should be a number')
elif price <= 0:
return('The price should be greater than 0')
elif discount < 0 or discount > 100:
return('The discount should be between 0 and 100')
else:
discount = (discount/100)*price
final_price = price - discount
return(final_price)
apply_discount(100,20)
```
What is your function returning when you test?
It returns nothing on any values I enter while calling function.
Please wrap your function call in print() so you can see what your function is returning.
in the return code remove the ()
After wrapping function call in print(), it only returns ‘The price should be a number’ for any values I test it with.
Yes. Why do you suppose that is?
Got it!
Because it was supposed to be if not isinstance(price,(float,int)).
Now it works.
Thank you

