未定义名称“总计”

时间:2019-05-02 23:28:13

标签: python-3.6

我应该有什么不同?结果是第12行print(total)NameError:未定义名称“ total”

def gross_pay (hours,rate):

   info =()
   info = getUserInfo()
   rate = float(input('How much do you make an hour?:'))
   hours = int(input('How many hours did you work?:'))
   total = rate * hours
   taxes = total * 0.05
   total = total - taxes

print(total)    

2 个答案:

答案 0 :(得分:0)

total是局部变量。它在函数外部不存在。另外,您需要调用该函数,您可以在其中返回合计。 getUserInfo()不存在,info未使用。在函数中询问输入参数也是不正确的。从技术上讲,税后工资是净工资,而不是毛工资:

def net_pay(hours,rate):
   total = rate * hours
   taxes = total * 0.05
   return total - taxes

rate = float(input('How much do you make an hour? '))
hours = int(input('How many hours did you work? '))
print(net_pay(hours,rate))

输出:

How much do you make an hour? 10.50
How many hours did you work? 40
399.0

答案 1 :(得分:-1)

def gross_pay (hours,rate):

   info =()
   # getUserInfo() should also be defined on your code:
   info = getUserInfo()
   rate = float(input('How much do you make an hour?:'))
   hours = int(input('How many hours did you work?:'))
   total = rate * hours
   taxes = total * 0.05
   total = total - taxes

   print(total)    

#calling the declarated (defined) function:
hours=0
rate=0
gross_pay()

我假设您要通过引用传递参数小时费率,因为稍后您将需要这些值,否则就不必使用它们了,因为您需要在 gross_pay 函数

中输入信息