Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
875 views
in Technique[技术] by (71.8m points)

python - nameError name is not defined

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...