试图通过用户的输入创建一个对象

我试图通过用户输入创建一个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"

你当前的代码有一个永无止境的循环,因为__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都是一个字符串,但您可能需要数小时并以浮点形式付费; 有关输入验证的更多信息,请参见此处。


没有运行的原因是因为你已经定义了一个类,但是没有任何东西正在实例化这个类,所以当你运行它时没有任何反应。

但是,如果您要添加该代码来实例化一个类,请说:

e = employee()
e.create_employee()

你会遇到由代码的循环性质引起的错误,正如@jonrsharpe所述。

您可以将create_employees移到类外部,并将其作为一个包含输入的包装器(包含常规变量或dict ,但不包括类本身),并使用该输入实例化对象。 然后您可以从脚本的主要部分调用该函数。

我建议您阅读关于类的Python文档,以便您可以更熟悉OO范例:

https://docs.python.org/2/tutorial/classes.html


我想你想要这样的事情

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()
链接地址: http://www.djcxy.com/p/54283.html

上一篇: Trying to create an object via a user's input

下一篇: Creating a Python classmethod