How to find a variable from another function in the function’s IF?

Asked

Viewed 61 times

-1

Good morning,

This program that I am practicing, already has other answers, however I decided to increase in another way. The program asks to request the value of the working hour and the amount worked in the month, from there perform the calculations and print the results. After a lot of catching, the program stopped giving error and even printed part of the statement (only of the global variables) and soon after presented an undefined variable error, as below:


Informe o nome completo do funcionário: José das Quantas
Informe o valor do salário por hora: 45
Informe as horas trabalhadas no mês: 34
  
###################################################################################
                              EXTRATO BASE DE SALÁRIO
###################################################################################
-----------------------------------------------------------------------------------
Funcionário: José das Quantas
                     Salário Bruto: (45.0 * 34.0)        : R$ 1530.0
-----------------------------------------------------------------------------------
Traceback (most recent call last):
  File "f:/Formação Acadêmica/Tecnologia/Udemy/Python/estruturaDeDecisao.py", line 61, in <module>
NameError: name 'impostoIR' is not defined

All the code described is this and the errors are in the PRINT function:

nome = str(input('Informe o nome completo do funcionário:'))
valorHora = float(input(' Informe o valor do salário por hora: '))
horasTrabalhadas = float(input('Informe as horas trabalhadas no mês: '))
salarioBruto = (valorHora * horasTrabalhadas)

def calculoDescontos(salarioBruto):
    descontoIR = 0
    inss = salarioBruto * 0.10
    fgts = salarioBruto * 0.11
    sindicato = salarioBruto * 0.03
    impostoIR = 0
    if salarioBruto <= 900:
        descontoIR = 'Isento'
        totalDesconto = sindicato + inss
        salarioLiquido = salarioBruto - totalDesconto
    elif salarioBruto > 900 and salarioBruto <= 1500:
        impostoIR = 5
        descontoIR = salarioBruto * 0.05
        totalDesconto = sindicato + inss + descontoIR
        salarioLiquido = salarioBruto - totalDesconto
    elif salarioBruto > 1500 and salarioBruto <= 2500:
        impostoIR = 10
        descontoIR = salarioBruto * 0.1
        totalDesconto = sindicato + inss + descontoIR
        salarioLiquido = salarioBruto - totalDesconto
    else:
        impostoIR = 20        
        descontoIR = salarioBruto * 0.2
        totalDesconto = sindicato + inss + descontoIR
        salarioLiquido = salarioBruto - totalDesconto

    imprimir(salarioBruto, sindicato, descontoIR, totalDesconto, fgts, salarioLiquido, impostoIR, inss)

def imprimir(salarioBruto, sindicato, descontoIR, totalDesconto, fgts, salarioLiquido, impostoIR, inss):
    print('  ')
    print('###################################################################################')
    print('                              EXTRATO BASE DE SALÁRIO                              ')
    print('###################################################################################')
    print('-----------------------------------------------------------------------------------')
    print('Funcionário: {} '.format(nome))
    print('-----------------------------------------------------------------------------------')
    print('                     Salário Bruto: ({} * {})        : R$ {} '.format(valorHora, horasTrabalhadas, salarioBruto))
    print('-----------------------------------------------------------------------------------')
    print('    (-) IR ({}%)                    : R$  {} '.format(impostoIR, descontoIR))
    print('    (-) INSS ( 10%)                 : R$  {} '.format(inss))
    print('    (-) SINDICATO ( 3%)             : R$  {} '.format(sindicato))
    print('    FGTS (11%)                      : R$  {} '.format(fgts))
    print('    Total de descontos              : R$  {} '.format(totalDesconto))
    print('-----------------------------------------------------------------------------------')
    print('                                     Salário Liquido                 : R$  {}  '.format(salarioLiquido))
    print('-----------------------------------------------------------------------------------')
    print('  ')

This is the first time I’ve ever posted on this site, so if there’s anything missing or not figured out, let me know to fix it.

Thanks for your help!

  • The definition of the print function requires 8 parameters, but when you call it you put only 7.

  • It was missing even, but it didn’t help. Just requests and reads the 3 input s.

  • You don’t need to put "solved" in the title. I know it’s common in many forums, but here it works different. In your case, as you yourself found the solution, enough mark your reply below as accepted, That’s enough to indicate that it’s been resolved.

  • Got it, thanks! After the stipulated deadline I will mark as solved then. Vlw.

1 answer

0


Analyzing another code, I found that failed to call the function after the last print:

calculoDetails(salarioBruto)

Now the program ran and with due formatting. Thank you Aviana by the missing parameter touch.

Thanks!

Browser other questions tagged

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