Python误差计算程序

发布于 2024-09-30 15:13:00 字数 1378 浏览 0 评论 0原文

要求用户输入公司的工资信息。设置一个循环,继续询问信息,直到他们输入“完成”。对于每位员工询问三个问题:

姓名(名字和姓氏) 本周工作小时数(仅允许 1 到 60 小时) 小时工资(仅允许 6.00 至 20.00) 验证工作时间和小时工资,并确保输入姓名。

计算每个员工的工资,并将其写入顺序文件。请务必包含文件 I/O 错误处理逻辑。

仅包括每周工资 周工资计算方式: 对于(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

Ask the user to enter payroll information for the company. Set up a loop that continues to ask for information until they enter “DONE”. For each employee ask three questions:

name (first & last)
hours worked this week (only allow 1 through 60)
hourly wage (only allow 6.00 through 20.00)
VALIDATE the hours worked and the hourly wage, and make sure a name is entered.

Calculate each employee’s pay, and write it out to a sequential file. Be sure to include file I/O error handling logic.

Include only the weekly pay
Weekly pay is calculated:
For (1-40 hours) it is hourly rate * hours worked
For (41-60 hours) it is (hours worked – 40) * (hourly rate * 1.5)
+ hourly rate * 40

After all the employees are entered, read in the sequential file into a list named PAY for the weekly pay of each employee. Sort the list. Now print the lowest, highest, and average weekly pay for the week.

I am having obvious problem with this code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青朷 2024-10-07 15:13:00

这段代码有几个问题:

  • 缩进到处都是。例如,while 循环在第一个 if 语句处结束。
  • while 循环的测试几乎肯定是 false(因为 eName 未初始化),因此循环永远不会进入
  • ePass.upper() == " 处的代码DONE”正在尝试设置 ePass 变量,这意味着测试将不起作用。您需要:

    如果 ePass.upper() == "DONE":
    中断

There's several problems with this code:

  • The indentation is all over the place. For example, the while loop ends at that first if statement
  • The test for the while loop is almost certainly false (since eName isn't initialised), so the loop never enters
  • the code at ePass.upper() == "DONE" is trying to set the ePass variable, which means that test won't work. You need:

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

铁憨憨 2024-10-07 15:13:00

试试这个:

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

它仍然缺乏异常检查,但应该可以工作。

“数据错误”检查应该只是短路主循环,它更简单,但您可以有更复杂的代码并将它们放入自己的循环中。

try this:

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

It still lacks exception checking but should work.

The "data error" checks should just short-circuit the main loop, it's simpler, but you can have a more involved code and put them into their own loop.

小耗子 2024-10-07 15:13:00

已经指出的一些错误:

在Python中,缩进决定代码块

while循环:

while logic_test:
    # this is inside while loop
    ....
# this is outside while loop

字符串上的某些函数不会就地替换字符串,它们通过返回值返回另一个字符串< /strong>

upper:

>>> a = "done"
>>> a.upper()
'DONE'
>>> a
'done'
>>> 

始终初始化变量。

如果您使用序列方法,则变量应该早先定义为序列。

>>> t.append('ll')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
>>> t = []
>>> t.append('ll')
>>> 

明确你的范围

lsthours = []
while len(eName)>0:
    ........
    lsthours.append(grossPay)

A few errors as has been pointed out:

In python, indentation decides the code blocks

while loop:

while logic_test:
    # this is inside while loop
    ....
# this is outside while loop

Certain functions on string does not replace the string in place, they return another string via return value

upper:

>>> a = "done"
>>> a.upper()
'DONE'
>>> a
'done'
>>> 

Always initialize your variables.

If you are using sequence methods, the variable should have been defined as sequence earlier.

>>> t.append('ll')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
>>> t = []
>>> t.append('ll')
>>> 

Make your scope explicit

lsthours = []
while len(eName)>0:
    ........
    lsthours.append(grossPay)
━╋う一瞬間旳綻放 2024-10-07 15:13:00

Yu 可以这样做:

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): ")

Yu can do something as this:

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): ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文