I really have no clue how to continue. Where could I read how to express these types of functions? I do not know yet how to express in python syntax the parts of the assignment.
W3 schools? The Python Tutorial — Python 3.14.3 documentation ? https://realpython.com/?
Khan Academy?
Udemy?
This is clearly a challenge where the previous steps do not provide me with the ability to know the syntax for the various steps. Maybe that aint the point 100%? Any guidance for where I can dig would be helpful.
Your code so far
def apply_discount(price, discount):
return(price*discount)
if price !=-(int or float):
print("The price should be a number")
elif discount !=(int or float):
print("The discount should be a number")
elif price <=0:
print("The price should be greater than 0")
elif discount <0 >100:
print("The discount should be between 0 and 100")
if apply_discount + price <0:
print("The price should be greater than 0")
elif apply_discount + discount <0 or >100:
print("The discount should be between 0 and 100")
apply_discount(100, 20)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Build an Apply Discount Function - Build a Discount Calculator
one thing it seems you are missing is that the return stops the function, that means that effectively your function has only one line inside and everything else is ignored
you can combine expressions in Python, but keep in mind that each part of the expression is checked, so you have discount < 0 first, and then 0 > 100 which is always False
here you are using apply_discount, remember that this is the name of the function, so you should not use it inside the function
here you also have or > 100, but each side of the or needs to have a full expression, meaning that you are missing one of the operands for >
if you have a price of 100 and a discount of 10%, you should get a final price of 90
I have made the basic calculation function and the printing part..
Now for the logic..
Reading through the notes about isinstance, getting some signals but still out of:
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.
Failed:4. When apply_discount is called with a discount (second argument) that is not a number (int or float) it should return The discount should be a number.
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.
Failed: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.
I have been attempting different ways to add in the conditionals but I generate more error messages. Instead I started with creating a functioning calculation. Then what I have left is to add conditionals.. been reading through the notes about conditionals and functions but adding them together is the next step that I am tinkering with
def apply_discount(price, discount):
if price <= 0:
print("The discount should be between 0 and 100")
elif price >= 100:
print("The discount should be between 0 and 100")
calc_discount = price * (1-discount/100)
print("The discount price is ", calc_discount)
return price*(1-discount/100)
Are you trying to validate price or discount, which?
If the number is equal to 0 or equal to 100, are you sure you want to print a validation error message?
Should you be printing the validation error message or returning it?
Hello,
I am trying to validate in accordance with the criteria of the task:
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.
And as you can see, I have no idea as I am a complete beginner in Python. Not sure of how to express this yet and in what order. Any links or files you can recommend for me to read through?
You can review this theory lecture again, which talks about using type(), and at the bottom, isinstance(). What is doesn’t show is that isinstance() can take a tuple as the second argument so you can check two or more types at once.
where would such a conditional be placed? Do not really see how to express it yet.
When I try to add in the tuple “apply_discount” nothing happens. def apply_discount (price, discount):
*calc_discount* = *price*\*(1-*discount*/100)
*ver_discount* = (*price*, *discount*)
if isinstance(*ver_discount*, int) != True:
print("The price should be a number")
if isinstance(*ver_discount*, float) != True:
print("The price should be a number")
return *calc_discount*
print ("The discount price is: ", apply_discount (100, 20))
No it seems like it is added to my post when I copy it to this forum for some reason.
Here is my code as I can make it at the moment
def apply_discount (price, discount):
calc_discount = price*(1-discount/100)
ver_discount = (price, discount)
if isinstance(ver_discount, int or float):
print("The price should be a number")
if ver_discount < 0 :
print("The price should be greater than 0")
if ver_discount > 100:
print("The price should be greater than 0")
return calc_discount
The syntax should be according to Free code camp:
isinstance(‘Hello world’, str) # True
isinstance(True, bool) # True
isinstance(42, int) # True
isinstance(‘John Doe’, int) # False
Okay, so if the second argument can be a tuple containing multiple types, how would you check if discount is an int or float? How would you write that?
def apply_discount ( price, discount):
calc_discount = price * (1-discount/100)
ver_price_int = isinstance(price, int)
ver_price_float = isinstance(price, float)
ver_price_num = price
ver_discount_num = discount
if ver_price_int != True:
print("The price should be a number")
if ver_price_float != True:
print("The price should be a number")
if ver_price_num <= 0:
print("The price should be greater than 0.")
if discount <0:
print ("The discount should be between 0 and 100")
if discount >100:
print("The discount should be between 0 and 100")
else:
print (apply_discount())
return calc_discount
print("The discount is ", apply_discount(100, -1))
I am not sure - to what I believe to be true when it comes to conditionals this is how I would do it.
Yet the “if” for discount or even the var I created to include isinstance dont work out.
Still points 3-6 are null, at the moment I am missing some pieces of how to construct the conditionals inside the function.
Does it make sense to calculate the discount before you validate price and discount?
Why are you creating new variables for your parameters?
Combine these into one if statement.
Use isinstance directly in your if statement. Creating variables like this adds no value and, as pointed out earlier, you can check if price is either an int or a float in one if statement that uses isinstance.