我正在使用Python 3进行一个小型个人项目,在其中您起了个名字并在那里获得了每周的工资。我想添加一个功能,管理员可以加入其中,并向工人添加一个人,包括他们的工资。到目前为止,我的情况如下:
worker = input("Name: ")
if worker == "Anne":
def calcweeklywages(totalhours, hourlywage):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
def main():
hours = float(input('Enter hours worked: '))
wage = 34
total = calcweeklywages(hours, wage)
print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
.format(**locals()))
main()
elif worker == "Johnathan":
def calcweeklywages(totalhours, hourlywage):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
def main():
hours = float(input('Enter hours worked: '))
wage = 30
total = calcweeklywages(hours, wage)
print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
.format(**locals()))
main()
我想添加一个部分,如果有人键入代码或他们是管理员的话,它将允许他们添加一个人或编辑现有的人的信息。
答案 0 :(得分:1)
我不确定您打算如何部署它,但是即使从目前的编码角度来看,它也不会像您期望的那样运行。我猜您正在这样做只是为了学习。因此,让我指出几个地方,您已经了解了错误的基本知识,以及如何做到这一点的纯教学方法示例。请记住,我强烈建议不要在任何实际环境中使用它。
您在代码中两次定义了函数cap = cv2.VideoCapture('E:\\User\\Programming\\Python\\Work\\asd\\%d.jpg', cv2.CAP_IMAGES)
。实际上,只需定义一次即可。如果要使用该代码,则可以像在calcweeklywages
程序中那样使用它来调用它。该功能对您的两个工人完全相同,因此要获得不同的每周工资,您需要传递不同的工资。但是,您如何将他们各自的工资与他们的名字(或代码中的某些表示形式)联系起来?
这是使用面向对象编程的一个很好的例子。简短而有趣的入门书籍是here。至于代码,看起来像这样,
main()
编辑:对不起,我忘记了管理员部分。可是,
class Employee:
def __init__(self, Name, Wage = 0, Hours = 0):
self.Name = Name
self.Wage = Wage
self.Hours = Hours
def calcweeklywages(Employee, totalhours):
'''Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
'''
hourlywage = Employee.Wage
if totalhours <= 40:
totalwages = hourlywage * totalhours
else:
overtime = totalhours - 40
totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
return totalwages
# In your main body, you just test the functionality
EmployeeList = []
EmployeeList.append(Employee("Anne", 34))
EmployeeList.append(Employee("Johnathan", 30))
while(True):
action = input('Exit? (y/n): ')
if(action == 'y'):
break
else:
name = input('Enter the employee\'s name: ')
for Employee in EmployeeList:
if(Employee.Name == name):
Person = Employee
hours = int(input('Enter the number of hours worked: '))
print('Wages for', hours, 'hours at', Person.Wage,'per hour is', calcweeklywages(Person, hours))
但是同样,会话在不同的内核运行之间不是持久的。没有真正的“安全性”,这只是对Python面向对象代码的探索。请不要将此用于任何实际应用。所有这一切还有很多。您需要将其存储在安全的文件中,并具有一些GUI前端等。有很多明智的用户将指导您实现整个系统。一切顺利。干杯。