您当前的代码中有一个永无休止的循环,
__init__并且
create_employee彼此调用。您在初始化程序中为所有属性获取参数,然后忽略它们并要求用户输入,然后将其传递给初始化程序以获取新对象,该对象将忽略它并…
我认为您想要的是一个更像这样的结构:
class Employee(object): # PEP-8 name def __init__(self, name, pay, hours): # assign instance attributes (don't call from_input!) def __str__(self): # replaces emp_name, returns a string @property def weekly_total(self): return sum(self.hours) @classmethod def from_input(cls): # take (and validate and convert!) input return cls(name, pay, hours)
您可以像这样使用:
employee = Employee("John Smith", 12.34, (8, 8, 8, 8, 8, 0, 0))print str(employee) # call __str__要么:
employee = Employee.from_input()print employee.weekly_total # access property
请注意,我假设每天没有一个单独的列表/元组,而不是在不同的日期具有不同的实例属性。如果日期名称很重要,请使用字典
{'Monday': 7,...}。请记住,所有内容raw_input都是字符串,但是您可能想要几个小时并以浮点数付款;有关输入验证的更多信息,请参见此处。



