Python计算程序时的错误

0 投票
4 回答
1720 浏览
提问于 2025-04-16 06:34

让用户输入公司的工资信息。设置一个循环,持续询问信息,直到他们输入“DONE”。对于每个员工,问三个问题:

姓名(名和姓)

本周工作小时数(只能输入1到60之间的数字)

时薪(只能输入6.00到20.00之间的数字)

要验证输入的工作小时数和时薪,并确保输入了姓名。

计算每个员工的工资,并将结果写入一个顺序文件。确保包含文件输入输出的错误处理逻辑。

只计算每周的工资:

每周工资的计算方式是:

对于1到40小时,工资 = 时薪 * 工作小时数

对于41到60小时,工资 = (工作小时数 - 40) * (时薪 * 1.5) + 时薪 * 40

在所有员工信息输入完后,将顺序文件中的数据读入一个名为PAY的列表,记录每个员工的每周工资。对这个列表进行排序。然后打印出本周最低、最高和平均的每周工资。

我在这段代码上遇到了明显的问题。

while len(eName)>0:
     eName=raw_input("\nPlease enter the employees' first and last name. ")
     hWork=raw_input("How many hours did they work this week? ")
     hoursWork=int(hWork)
     if hoursWork < 1 or hoursWork > 60:
         print "Employees' can't work less than 1 hour or more than 60 hours!"

     else:
         pRate=raw_input("What is their hourly rate? ")
         payRate=int(pRate)
         if payRate < 6 or payRate > 20:
              print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
         if hoursWork <=40:
              grossPay=hoursWork*payRate
         else:
              grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate)
         lsthours.append(grossPay)
     print grossPay
     print lsthours



     ePass=raw_input("Type DONE when finished with employees' information. ")
     ePass.upper() == "DONE"
     if ePass == "DONE":
          break
     else:
          continue

4 个回答

1

你可以这样做:

grossPay = 0.0
lsthours = []

eName=raw_input("\nPlease enter the employees' first and last name (type 'PASS' to  exit): ") 

while eName.upper() != "PASS":      
   hWork=raw_input("How many hours did they work this week? ") 
   hoursWork=int(hWork)

   if hoursWork < 1 or hoursWork > 60: 
      print "Employees' can't work less than 1 hour or more than 60 hours!" 
   else: 
      pRate=raw_input("What is their hourly rate? ") 
      payRate=int(pRate) 

      if payRate < 6 or payRate > 20: 
         print "Employees' wages can't be lower than $6.00 or greater than $20.00!" 

      if hoursWork <=40: 
         grossPay=hoursWork*payRate 
      else: 
         grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate) 

      lsthours.append(grossPay) 

      print grossPay 
      print lsthours 

  eName=raw_input("\nPlease enter the employees' first and last name. (type 'PASS' to exit): ")
2

这段代码有几个问题:

  • 缩进乱七八糟的。比如,while循环在第一个if语句那里就结束了。
  • while循环的条件几乎肯定是假的(因为eName没有被初始化),所以循环根本不会执行。
  • 代码中的ePass.upper() == "DONE"试图设置ePass变量,这样的话这个测试就不管用了。你应该这样写:

    if ePass.upper() == "DONE":
        break

1

试试这个:

lsthours = list()
eName = "start" # initialize to something to start the loop
while eName:
    eName = raw_input("\nPlease enter the employees' first and last name. ")
    if not eName:
        break #loop will exit also when blank name is inserted
    hWork = raw_input("How many hours did they work this week? ")
    hoursWork = int(hWork)
    if hoursWork < 1 or hoursWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue #skip

    pRate = raw_input("What is their hourly rate? ")
    payRate = int(pRate)
    if payRate < 6 or payRate > 20:
        print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
        continue #skip
    if hoursWork <= 40:
        grossPay = hoursWork * payRate
    else:
        grossPay = ((hoursWork - 40) * (payRate * 1.5)) + (40 * payRate)
    lsthours.append(grossPay)
    print grossPay
    print lsthours
    ePass = raw_input("Type DONE when finished with employees' information. ")
    if ePass.upper() == "DONE":
        break

这段代码虽然还没有处理异常情况,但应该可以正常运行。

关于“数据错误”的检查,可以直接跳过主循环,这样更简单。不过你也可以写得复杂一点,把这些检查放到自己的循环里。

撰写回答