Trying to create an object via a user's input

后端 未结 3 1275
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 15:36

I\'m trying to create an employee object via user input, but I\'m running into issues with my code. When I run this nothing happens and it\'s not throwing any e

相关标签:
3条回答
  • 2021-01-07 16:13

    Your current code has a neverending loop in it, as __init__ and create_employee call each other. You take arguments for all the attributes in the initialiser, then ignore them and ask for user input, which you pass to the initialiser for a new object, which ignores it and...

    I think what you want is a structure more like:

    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)
    

    Which you can use like:

    employee = Employee("John Smith", 12.34, (8, 8, 8, 8, 8, 0, 0))
    print str(employee) # call __str__
    

    Or:

    employee = Employee.from_input()
    print employee.weekly_total # access property
    

    Note that rather than having separate instance attributes for the different days, I've assumed a single list/tuple of hours for each day. If the day names are important, use a dictionary {'Monday': 7, ...}. Remember that all raw_input is a string, but you probably want hours and pay as floats; for more on input validation, see here.

    0 讨论(0)
  • 2021-01-07 16:24

    I think you want something like this

    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()
    
    0 讨论(0)
  • 2021-01-07 16:28

    The reason this isn't running is because you've defined a class, but then there is nothing that is instantiating that class, so nothing happens when you run it.

    However, if you were to add that code to instantiate a class, say:

    e = employee()
    e.create_employee()
    

    you would run into errors caused by the circular nature of the code, as mentioned by @jonrsharpe.

    You could move create_employees outside of the class and have it be a wrapper which takes in input (into regular variables, or a dict, but not the class itself), and instantiates the object using that input. You could then call that function from the main part of the script.

    I would recommend reading through the Python doc on classes so you can become more familiar with the OO paradigm:

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

    0 讨论(0)
提交回复
热议问题