Doubt called function by loop

Asked

Viewed 30 times

0

Good afternoon, I’m starting programming and I’m having a question about calling function through loop. I created the function and I’m trying to put so that at the end, ask if you want to continue, if I press 0, the loop stops however, pressing qlq another number, it stays in loop without giving result.

def somaimposto(taxaimposto, custo):
    return (1 + taxaimposto/100)*custo
    
taxaimposto = 10
custo = float(input('Qual o custo do produto: '))

print('O valor a ser vendido é de', somaimposto(taxaimposto, custo))

res = 1

while res:
    if(res):
        somaimposto(taxaimposto, custo)
    res = int(input("Digite 0 se desejar encerrar ou qualquer outro numero para continuar: "))
  • 2

    Within your while has not print so it’s not working

2 answers

0


Good afternoon Vinicius, if I understand correctly you want to calculate the value to be sold based on the cost of the product.

Your code didn’t work because it didn’t put the essential parts inside the while loop. Your program works correctly with the following modification:

def somaimposto(taxaimposto, custo):
    return (1 + taxaimposto/100)*custo
    

taxaimposto = 10
res = 1

while res:                # veja que movi esta parte que estava fora para dentro do loop
    if res:                                 
        custo = float(input('Qual o custo do produto: ')) 
        print('O valor a ser vendido é de', somaimposto(taxaimposto, custo))
        
    res = int(input("Digite 0 se desejar encerrar ou qualquer outro numero para continuar:")) 

I hope I’ve helped!

0

To solve this issue you can use the following code:

def somaimpostos(taxa, cust):
    return (1 + taxa/100)*cust


while True:
    taxa_imposto = 10
    custo = float(input('Qual o custo do produto? '))

    print(f'R$ {somaimpostos(taxa_imposto, custo):.2f}')

    resp = input('Para encerrar digite "0" e qualquer outro valor para continuar! ')
    if resp == '0':
        break

Note that when we execute the code we receive the following message: Qual custo do produto? . At this point we must enter the cost of the product and press enter. Then the code will calculate and display the result. Then we receive the following message: Para encerrar digite "0" e qualquer outro valor para continuar! . At this point if we want to close we must press "0" and press enter. If we want to continue just press any key and press enter.

  • 1

    Thank you very much friend. Now I understand!

  • @Vinicius Araújo, Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.