# Crie uma função para aplicar desconto - Criar uma função Apply Discount

**URL:** <https://forum.freecodecamp.org/t/crie-uma-funcao-para-aplicar-desconto-criar-uma-funcao-apply-discount/790000>\
**Category:** General\
**Tags:** português\
**Created:** [May 27, 2026, 2:09pm UTC](https://forum.freecodecamp.org/t/crie-uma-funcao-para-aplicar-desconto-criar-uma-funcao-apply-discount/790000 "2026-05-27T14:09:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luizao4k](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/luizao4k/32/384634_2.png) [@luizao4k](https://forum.freecodecamp.org/u/luizao4k)\
**Post date:** [May 27, 2026, 2:09pm UTC](https://forum.freecodecamp.org/t/crie-uma-funcao-para-aplicar-desconto-criar-uma-funcao-apply-discount/790000/1 "2026-05-27T14:09:57Z")

</div>

### Conte-nos o que está acontecendo:

Quando `apply_discount` é chamado com um `price` (primeiro argumento) que não é um número (`int` ou `float`) ele deve retornar `The price should be a number`.

### Seu código até o momento

```py
def verificar_preco(x):
    if type(x) is bool or type(x) is str:
        return "The price should be a number."
    if x < 0:
        return "The price should be greater than 0."
    return True
def verificar_desconto(y):
    if type(y) is bool or type(y) is str:
        return "The discount should be a number."
    if y < 0 or y > 100:
        return "The discount should be between 0 and 100."
    return True
def apply_discount(price, discount):
    ver1 = verificar_preco(price)
    ver2 = verificar_desconto(discount)
    
    if ver1 is True and ver2 is True: 
        x = (discount * price) / 100
        final = price - x
        print(f"Success! Final price: {final}")
        return final
    else:
     
        if type(ver1) is str:
            print(f"Error (Price): {ver1}")
        if type(ver2) is str:
            print(f"Error (Discount): {ver2}")
    print("-" * 30)

apply_discount(100, 20)
apply_discount(200, 50)
apply_discount(50, 100)
apply_discount(74.5, 20)
apply_discount(True, 20)
apply_discount(-5, 20)
apply_discount(5, -20)

```

### Informações do seu navegador:

Agente de usuário: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.0`

### Informações do desafio:

Crie uma função para aplicar desconto - Criar uma função Apply Discount

> **[freeCodeCamp.org](https://www.freecodecamp.org/portuguese/learn/python-v9/lab-discount-calculator/build-a-discount-calculator)**
>
> Aprenda a programar — de graça

Link do GitHub: [i18n-curriculum/curriculum/challenges/portuguese/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md at main · freeCodeCamp/i18n-curriculum · GitHub](https://github.com/freeCodeCamp/i18n-curriculum/blob/main/curriculum/challenges/portuguese/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md)

---

<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:** [May 27, 2026, 3:22pm UTC](https://forum.freecodecamp.org/t/crie-uma-funcao-para-aplicar-desconto-criar-uma-funcao-apply-discount/790000/2 "2026-05-27T15:22:35Z")

</div>

Welcome to the forum @luizao4k,

I highly recommend writing all of your code within one function.

> [@luizao4k](#):
>
> `if type(x) is bool or type(x) is str:`

What if price is in a list or a tuple or a set…? Does your code handle that?

Please take a look at how to use` isinstance` to check for multiple types at the end of this query lecture:

[Understanding Variables and Data Types - How Do the type() and isinstance() Functions Work? | Learn | freeCodeCamp.org](https://www.freecodecamp.org/learn/python-v9/lecture-understanding-variables-and-data-types/how-do-the-type-and-isinstance-functions-work)

> [@luizao4k](#):
>
> `return "The price should be a number."`

Should your validation messages have punctuation?

You can see what your function returns by wrapping your function calls in` print(`).

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:** [June 24, 2026, 3:23pm UTC](https://forum.freecodecamp.org/t/crie-uma-funcao-para-aplicar-desconto-criar-uma-funcao-apply-discount/790000/3 "2026-06-24T15:23:14Z")

</div>

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