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

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

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

Link do GitHub: i18n-curriculum/curriculum/challenges/portuguese/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md at main · freeCodeCamp/i18n-curriculum · GitHub

Welcome to the forum @luizao4k,

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

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

Should your validation messages have punctuation?

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

Happy coding

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