Python误差计算程序
要求用户输入公司的工资信息。设置一个循环,继续询问信息,直到他们输入“完成”。对于每位员工询问三个问题:
姓名(名字和姓氏) 本周工作小时数(仅允许 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这段代码有几个问题:
如果 ePass.upper() == "DONE":
中断
There's several problems with this code:
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
试试这个:
它仍然缺乏异常检查,但应该可以工作。
“数据错误”检查应该只是短路主循环,它更简单,但您可以有更复杂的代码并将它们放入自己的循环中。
try this:
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.
已经指出的一些错误:
在Python中,缩进决定代码块
while循环:
字符串上的某些函数不会就地替换字符串,它们通过返回值返回另一个字符串< /strong>
upper:
始终初始化变量。
如果您使用序列方法,则变量应该早先定义为序列。
明确你的范围
A few errors as has been pointed out:
In python, indentation decides the code blocks
while loop:
Certain functions on string does not replace the string in place, they return another string via return value
upper:
Always initialize your variables.
If you are using sequence methods, the variable should have been defined as sequence earlier.
Make your scope explicit
Yu 可以这样做:
Yu can do something as this: