Python 从一个函数到另一个函数使用局部变量

Python 从一个函数到另一个函数使用局部变量,python,python-3.x,function,variables,Python,Python 3.x,Function,Variables,编写一个程序,使用两个函数计算员工的周工资。一个函数计算工资。其他函数打印工资。如果员工加班,其加班工资应为一倍半。假设没有税收。用代码编写适当的注释。不要使用全局变量。 这就是我到目前为止所做的: def payrate(): hours = int(input('How many hours did you work?\n')) rate = int(input('What is your hourly rate?\n')) if hours <= 40:

编写一个程序,使用两个函数计算员工的周工资。一个函数计算工资。其他函数打印工资。如果员工加班,其加班工资应为一倍半。假设没有税收。用代码编写适当的注释。不要使用全局变量。 这就是我到目前为止所做的:

def payrate():
     hours = int(input('How many hours did you work?\n'))
     rate = int(input('What is your hourly rate?\n'))
     if hours <= 40:
         total = hours * rate
     else:
         total = 40 * rate + (hours - 40) * (1.5 * rate)

def salary():
     for total in payrate():
         print('Your weekly salary is $%d' %total)
         return payrate(total)
salary()
def payrate():
hours=int(输入('您工作了多少小时?\n'))
rate=int(输入('您的小时费率是多少?\n'))

如果时间干得好,你就非常接近了。您的薪资函数需要从payrate函数接收总薪资,payrate函数需要将总薪资返回到薪资,以便可以打印。这个代码对我来说很好:

    def payrate():

      hours = int(input('How many hours did you work?\n'))
      rate = int(input('What is your hourly rate?\n'))

      if hours <= 40 :
        total = hours * rate
      else :
        total = 40 * rate + (hours - 40) * (1.5 * rate)

      return total


    def salary():
      pay = payrate()
      print('Your weekly salary is $%d' % pay)


    salary()
def payrate():
hours=int(输入('您工作了多少小时?\n'))
rate=int(输入('您的小时费率是多少?\n'))
如果小时数这应该有效:

def payrate():
     hours = int(input('How many hours did you work?\n'))
     rate = int(input('What is your hourly rate?\n'))
     if hours <= 40:
         total = hours * rate
     else:
         total = 40 * rate + (hours - 40) * (1.5 * rate)
     return total

def salary():
     total = payrate()
     print('Your weekly salary is $%d' %total)
salary()
要从另一个函数接收它,可以在第二个函数中调用第一个函数,如下所示:

recieved_local_variable = first_function() 

在Python中,您可以将参数传递给定义中的函数。。。():例如,您可以将变量rate和hours传递给函数payrate,该函数返回您计算的总数:

def payrate(rate, hours):
    ... 
    return total
如果您想要一个名为salary的函数,它只打印payrate函数的结果,您可以执行以下操作:

def salary(hours, rate):
   print(payrate(hours, rate))
上面的函数调用payrate函数并打印返回值

然后用变量“hours”和“rate”简单地调用salary函数


希望这有帮助

谢谢大家的意见。这解决了我的问题。
def salary(hours, rate):
   print(payrate(hours, rate))
salary(hours, rate)