【发布时间】:2020-03-08 21:59:43
【问题描述】:
我接到了一项家庭作业,要编写一个 Python 程序,根据每小时工资率和工作小时数计算工人的工资。到目前为止,我已经想出了以下代码...
#Function
def calculatePay(rateHour,nHours):
if nHours <= 40:
pay = rateHour * nHours
elif nHours < 60:
pay = (rateHour * 40) + ((nHours - 40) * (rateHour * 1.5))
else:
pay = (rateHour * 40) + (20 * (rateHour * 1.5)) + ((nHours - 60) * (rateHour * 2.0))
return pay
#Main Code
pay1 = calculatePay(30, 20)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay1)
print()
pay2 = calculatePay(15.50, 50)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay2)
print()
pay3 = calculatePay(11, 70.25)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay3)
print()
rateHour = int(input('Enter the rate per hour: '))
nHours = int(input('Enter the number of hours worked: '))
pay4 = calculatePay(rateHour,nHours)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay4)
print()
当我运行它时,我收到以下错误...
Traceback (most recent call last):
File "C:\Users\John\Desktop\Python Programming\JohnLissandrello_Homework3.py", line 15, in <module>
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay1)
NameError: name 'nHours' is not defined
我相信这是因为我试图在我的主代码中使用局部变量 rateHour 和 nHours。
如何将这两个变量从我的函数传递到主代码中,以便我可以输出 rateHour 和 nHours 以及计算出的工资?
【问题讨论】: