我正在努力弄清楚我正在做的这项家庭作业的这个错误。它假设是一个计算工资和加班工资的工资计划,加班时间定义为40以上的任何小时数。
我的错误:
Traceback (most recent call last):
File "C:\Python33\Assignment6.py", line 79, in <module>
main()
File "C:\Python33\Assignment6.py", line 18, in main
original_pay = calculate_pay(original_hours, original_rate)
File "C:\Python33\Assignment6.py", line 52, in calculate_pay
original_hours = int(original_hours)
TypeError: int() argument must be a string or a number, not 'tuple'
我的代码:
overtime_rate_constant = .5
overtime_limit = 40
overtime_hours = 0
def main ():
greet()
original_hours = get_hours()
original_rate = get_rate()
original_pay = calculate_pay(original_hours, original_rate)
totalpay = overtime_pay + original_pay
print_data(original_rate, overtime_hours, original_hours, calculate_overtime_hours, original_pay, totalpay)
def get_hours ():
original_hours = int(input("Please enter hours worked (8-86): "))
print()
while original_hours < 8 or original_hours > 86:
print("You must enter between 8 - 86 hours.")
print()
original_hours = int(input("Please try again: "))
print()
original_hours = int(original_hours)
for original_hours in range (41, 86):
overtime_hours = original_hours - overtime_limit
return original_hours, overtime_hours
return original_hours
def get_rate ():
original_rate = int(input("Please enter your hourly rate: "))
print()
while original_rate < 7 or original_rate > 50:
print("You must enter a pay rate between 7 - 50 dollars")
print()
rate = int(input("Please try again: "))
print()
return original_rate
return original_rate
def calculate_pay (original_hours, original_rate):
original_hours = int(original_hours)
original_rate = int(original_rate)
original_pay = original_hours * original_rate
overtime_pay = original_hours * original_hours + overtime_hours * .5
return overtime_pay, original_pay
def print_data (original_rate, overtime_hours, original_hours, overtime_pay, original_pay, totalpay):
print()
print("Pay rate: $", format (original_rate, '0,.2f'))
print("Regular Hours: ", format (original_hours, '0,.0f'))
print("Overtime Hours: ", format (overtime_hours, '0,.0f'))
print("Regular Pay: $", format (original_pay, '0,.2f'))
print("Overtime Pay: $", format (overtime_pay, '0,.2f'))
print("Total Pay: $", format (totalpay, '0,.2f'))
def greet ():
print("This program calculates pay.")
print()
main()
答案 0 :(得分:2)
我没有看到这是给你的任务。无论如何,专注于变量赋值,然后如果你返回n值,那么从你调用的地方接收n个变量,你将得到以下错误:
TypeError: int() argument must be a string or a number, not 'tuple'
你的节目有点混乱。现在,它已经修复了!
overtime_rate_constant = .5
overtime_limit = 40
overtime_hours = 0
def main ():
greet()
original_hours,overtime_hours = get_hours()
original_rate = get_rate()
overtime_pay, original_pay = calculate_pay(original_hours, original_rate)
totalpay = overtime_pay + original_pay
print_data(original_rate, overtime_hours, original_hours, overtime_pay, original_pay, totalpay)
def get_hours ():
original_hours = int(input("Please enter hours worked (8-86): "))
print()
while original_hours < 8 or original_hours > 86:
print("You must enter between 8 - 86 hours.")
print()
original_hours = int(input("Please try again: "))
print()
#original_hours = int(original_hours)
for original_hours in range (41, 86):
overtime_hours = original_hours - overtime_limit
return original_hours, overtime_hours
#return original_hours
def get_rate ():
original_rate = int(input("Please enter your hourly rate: "))
print()
while original_rate < 7 or original_rate > 50:
print("You must enter a pay rate between 7 - 50 dollars")
print()
rate = int(input("Please try again: "))
print()
return original_rate
return original_rate
def calculate_pay (original_hours, original_rate):
original_hours = int(original_hours)
original_rate = int(original_rate)
original_pay = original_hours * original_rate
overtime_pay = original_hours * original_hours + overtime_hours * .5
return overtime_pay, original_pay
def print_data (original_rate, overtime_hours, original_hours, overtime_pay, original_pay, totalpay):
print()
print("Pay rate: $", format (original_rate, '0,.2f'))
print("Regular Hours: ", format (original_hours, '0,.0f'))
print("Overtime Hours: ", format (overtime_hours, '0,.0f'))
print("Regular Pay: $", format (original_pay, '0,.2f'))
print("Overtime Pay: $", format (overtime_pay, '0,.2f'))
print("Total Pay: $", format (totalpay, '0,.2f'))
def greet ():
print("This program calculates pay.")
print()
main()
你错过了程序中的不同内容。我整理出来了。我也在展示我修正的错误:
错误1:
Traceback (most recent call last):
File "p.py", line 72, in <module>
main()
File "p.py", line 11, in main
original_pay = calculate_pay(original_hours, original_rate)
File "p.py", line 45, in calculate_pay
original_hours = int(original_hours)
TypeError: int() argument must be a string or a number, not 'tuple'
错误2:
Traceback (most recent call last):
File "p.py", line 72, in <module>
main()
File "p.py", line 12, in main
totalpay = overtime_pay + original_pay
NameError: global name 'overtime_pay' is not defined
错误3:
Traceback (most recent call last):
File "p.py", line 73, in <module>
main()
File "p.py", line 14, in main
print_data(original_rate, overtime_hours, original_hours, calculate_overtime_hours, original_pay, totalpay)
NameError: global name 'calculate_overtime_hours' is not defined
答案 1 :(得分:1)
问题出在这一行(在get_hours
函数中):
return original_hours, overtime_hours
这将返回一个元组。
答案 2 :(得分:1)
您的calculate_pay()
功能无法正常处理加班。请务必正确传递get_hours()
返回的元组中的值,并确保get_hours()
始终返回2元组,即使第二个元素为0。 / p>