0

Hello I am trying to create a program where you input how many hours you worked and the rate per hour. Every hour over 40 is counted as over time (x1.5).

I am getting a nameError on line 11. ( ganancias = (horas * tarifa) NameError: name 'horas' is not defined)

I dont understand why since I defined "horas" in the second line. Thanks for your time!

def calculo_salario() :
  horas = float(input("input salario: "))
  tarifa = float(input("input tarifa: "))

def sums(a,b):
  sum = a + b
  return sum

calculo_salario()

ganancias = (horas * tarifa)
preOt = (40 * tarifa)



if horas > 40 :
    overtimeHr = horas - 40
    overtimeAm = (overtimeHr * tarifa) * 1.5
    gananciasOt = sums(overtimeAm, preOt)
    print(gananciasOt)

else :
    print(ganancias)

3 Answers3

0

Your issue is on the scope of horas. You're defining horas as a local variable in the method calculo_salario. This means that the variable will only be available inside the method definition and not outside.

I'd suggest starting out your program without any methods and then figure out how it would work by encapsulating logic within these methods.

horas = float(input("input salario: "))
tarifa = float(input("input tarifa: "))

ganancias = (horas * tarifa)
preOt = (40 * tarifa)

if horas > 40 :
    overtimeHr = horas - 40
    overtimeAm = (overtimeHr * tarifa) * 1.5
    gananciasOt = overtimeAm + preOt
    print(gananciasOt)

else :
    print(ganancias)

The code above should run without any issues notice that now all the variables are in the global scope of the script.

Leo Correa
  • 19,131
  • 2
  • 53
  • 71
  • oohh okay good to know! The thing is im doing an exercise that requires me to define "calculo_salario" and work from there. Ive already created the alternate program without creating any new definitions. I hope that makes sense.. – Eduardo Jose Nov 26 '17 at 03:27
  • You could potentially create the `horas`/`tarifa` variables outside of the method or have them returned by the method as @jhpratt metioned in his answer – Leo Correa Nov 26 '17 at 03:29
0

Check out variable scope. Right now, horas and tarifa are being dropped as soon as calculo_salario() is finished.

To fix this, one option is to return the values. Of course, in this specific instance you don't need a method at all, but that's not the point.

def calculo_salario() :
  horas = float(input("input salario: "))
  tarifa = float(input("input tarifa: "))
  return (horas, tarifa)

...

(horas, tarifa) = calculo_salario()

...
jhpratt
  • 6,841
  • 16
  • 40
  • 50
0

I used jhpratt's suggestion in using the return to assign those values to "calculo_salario()" here is my final working code

def calculo_salario() :
  horas = float(input("input salario: "))
  tarifa = float(input("input tarifa: "))
  return (horas, tarifa)


(horas, tarifa) = calculo_salario()
ganancias = (horas * tarifa)
preOt = (40 * tarifa)



if horas > 40 :
    overtimeHr = horas - 40
    overtimeAm = (overtimeHr * tarifa) * 1.5
    gananciasOt = (overtimeAm + preOt)
    print("salario: " , gananciasOt)

else :
    print("salario:" , ganancias)