我有这个程序,我正在尝试写,我不能让它停止一遍又一遍地询问输入。我的错误在哪里?任何帮助都可以;我是编程的新手,所以这都是外国的。
def main():
hours,rate = getinput()
strtime,overtimehr,overtime = calculate_hours(hours)
regular,overtime,totalpay = calculate_pay(regular,overtimehr,rate)
def getinput():
print ()
print ('How many hours were worked?')
print ('Hours worked must be at least 8 and no more than 86.')
hours = float(input('Now enter the hours worked please:'))
while hours < 8 or hours > 86: #validate the hours
print ('Error--- The hours worked must be atleast 8 and no more than 86.')
hours = float(input('Please try again:'))
print ('What is the pay rate?')
print ('The pay rate must be at least $7.00 and not more than $50.00.')
rate = float(input('Now enter the pay rate:'))
while rate < 7 or rate > 50: #validate the payrate
print ('Error--- The pay rate must be at least $7.00 and not more than $50.00.')
rate = float (input('Please try again:'))
return hours, rate
getinput()
def calculate_hours(hours):
if hours < 40:
strtime = hours
overtime = 0
else:
strtime = 40
overtimehr = hours - 40
if hours > 40:
overtimerate = 1.5 * rate
overtime = (hours-40) * overtimerate
hours = 40
else:
overtime = 0
return strtime, overtime, overtimehr
calculate_hours(hours)
def calculate_payregular(regular,totalpay,rate):
regular = hours * rate
totalpay = overtime + regular
return regular, totalpay,rate
calculate_payregular(regular,totalpay,rate)
def calprint (rate, strtime, overtimehr, regular, overtime, totalpay):
print (" Payroll Information")
print ()
print ("Pay rate $", format (rate, '9,.2f'))
print ("Regular Hours ", format (strtime, '9,.2f'))
print ("Overtime hours ", format (overtimehr, '9,.2f'))
print ("Regular pay $", format (regular, '9.2f'))
print ("Overtime pay $", format (overtime, '9.2f'))
print ("Total Pay $", format (totalpay, '9.2f'))
calprint (rate, strtime, overtimehr, regular, overtime, totalpay)
main ()
答案 0 :(得分:1)
您的所有功能(包括getinput()
)似乎都在最后调用自己,这可能是您遇到问题的原因。在任何一个函数中你都不需要最后一行 - 我不确定你为什么这么认为。将它从所有这些中删除。
此外,我希望return hours, rate
应该缩进一个级别。