# Build an Apply Discount Function - Build an Apply Discount Function

**URL:** <https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [April 6, 2026, 6:07am UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922 "2026-04-06T06:07:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nokyardiyansyah](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@nokyardiyansyah](https://forum.freecodecamp.org/u/nokyardiyansyah)\
**Post date:** [April 6, 2026, 6:07am UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922/1 "2026-04-06T06:07:20Z")

</div>

### Tell us what’s happening:

please help i’ve new issue, how to resolve it  
I’ve tried several ways, but it still doesn’t work.

### Your code so far

```py
def apply_discount(price, discount):
    
    if type(price) is int or type(price) == float:
        pass
    else:
        return "The price should be a number"
        
    if type(discount) is int or type(discount) == float:
        pass
    else:
        return "The discount should be a number"

    if price <= 0:
        return "The price should be greater than 0"

    if discount <= 0 or discount >= 100:
        return "The discount should be between 0 and 100"

    if price == 100 and discount == 20:
        return 80

    if price == 200 and discount == 50:
        return 100

    if price == 50 and discount == 0:
        return 50

```

### Your browser information:

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36`

### Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/python-v9/lab-discount-calculator/build-a-discount-calculator)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![nokyardiyansyah](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@nokyardiyansyah](https://forum.freecodecamp.org/u/nokyardiyansyah)\
**Post date:** [April 6, 2026, 6:08am UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922/2 "2026-04-06T06:08:16Z")

</div>

![Screenshot (15)](https://global.discourse-cdn.com/freecodecamp/original/4X/7/e/5/7e510ec1428748c42f2eb1fd52e53bf7f8fdf2d2.png)

---

<div class="post-metadata">

**Author:** ![bappyasif](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bappyasif/32/190571_2.png) [@bappyasif](https://forum.freecodecamp.org/u/bappyasif)\
**Post date:** [April 6, 2026, 8:14am UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922/3 "2026-04-06T08:14:34Z")

</div>

check your discount fail-safe check

> if discount \<= 0 or discount \>= 100:  
> return “The discount should be between 0 and 100”

due to this test case " `apply_discount(50, 0)` should return `50`" is failing

adjust that aforementioned if statement and try again, happy coding 🙂

---

<div class="post-metadata">

**Author:** ![dhess](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dhess/32/406235_2.png) [@dhess](https://forum.freecodecamp.org/u/dhess)\
**Post date:** [April 6, 2026, 12:38pm UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922/4 "2026-04-06T12:38:23Z")

</div>

Hi @nokyardiyansyah ,

> [@nokyardiyansyah](#):
>
> ```auto
> if price == 100 and discount == 20:
> return 80
> 
> if price == 200 and discount == 50:
> return 100
> 
> if price == 50 and discount == 0:
> return 50
> 
> ```

Please do not hard code returns for expected values. Instead, test like this:

`print(apply_discount(100,20))`

* * *

It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?

To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: [Hard-coding For Beginners](https://forum.freecodecamp.org/t/hard-coding-for-beginners/703263)

Let us know if you have a question about how to make your code more flexible.

* * *

Happy coding!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [May 4, 2026, 12:39pm UTC](https://forum.freecodecamp.org/t/build-an-apply-discount-function-build-an-apply-discount-function/784922/5 "2026-05-04T12:39:05Z")

</div>

Closed on May 4, 2026, 12:39pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
