我正在尝试通过用户输入创建一个employee
对象,但我遇到了代码问题。当我跑步时,没有任何事情发生,它也没有抛出任何错误。
class employee(object):
def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday):
self.create_employee()
self.name = name
self.pay_rate = pay_rate
self.monday = monday
self.tuesday = tuesday
self.wednesday = wednesday
self.thursday = thursday
self.friday = friday
self.saturday = saturday
self.sunday = sunday
def weekly_total(self):
self.total_weekly_hours = self.monday + self.tuesday + self.wednesday + self.thursday + self.friday + self.saturday + self.sunday
self.emp_name()
print "\n Your hours this week are:", self.total_weekly_hours,"\n"
def emp_name(self):
print "\n Current employee is: ",self.name
def create_employee(self):
self.name = raw_input("Enter new employee name")
self.pay = input("Enter pay rate")
self.monday = raw_input("Enter monday hours")
self.tuesday = raw_input("tuesday hours?")
self.wednesday = raw_input("wed hours?")
self.thursday = raw_input("Thursday hours?")
self.friday = raw_input("Friday hours?")
self.saturday = raw_input("saturday hours?")
self.sunday = raw_input("sunday hours?")
self.object_name = raw_input("Name your object")
self.object_name = employee(self.name,self.pay,self.monday,self.tuesday,self.wednesday,self.thursday,self.friday,self.saturday,self.sunday)
print self.name, " was created"
答案 0 :(得分:1)
这个没有运行的原因是因为你已经定义了一个类,但是没有什么可以实例化那个类,所以当你运行它时没有任何反应。
但是,如果您要添加该代码来实例化一个类,请说:
e = employee()
e.create_employee()
如@jonrsharpe所述,您会遇到由代码的循环性质引起的错误。
你可以将create_employees
移到类之外,让它成为一个包装器,它接受输入(转换为常规变量,或dict
,但不是类本身),并使用它来实例化对象那个输入。然后,您可以从脚本的 main 部分调用该函数。
我建议您阅读类上的 Python 文档,这样您就可以更熟悉 OO 范例了:
答案 1 :(得分:1)
您当前的代码中有一个无休止的循环,因为__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
都是一个字符串,但您可能需要数小时并支付浮点数;有关输入验证的更多信息,请参阅here。
答案 2 :(得分:0)
我想你想要这样的东西
class employee(object):
def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday):
self.name = name
self.pay_rate = pay_rate
self.monday = monday
self.tuesday = tuesday
self.wednesday = wednesday
self.thursday = thursday
self.friday = friday
self.saturday = saturday
self.sunday = sunday
@staticmethod
def create_from_rawinput():
return employee(
raw_input("Employee name:"),
raw_input("Pay Rate:"),
raw_input("Enter monday hours:"),
raw_input("Enter tuesday hours:"),
raw_input("Enter wednesday hours:"),
raw_input("Enter thursday hours:"),
raw_input("Enter friday hours:"),
raw_input("Enter saturday hours:"),
raw_input("Enter sunday hours:")
)
new_emp = employee.create_from_rawinput()