尝试通过用户的输入创建对象 [英] Trying to create an object via a user's input
问题描述
我试图通过用户输入创建一个员工
对象,但我遇到了我的代码的问题。当我运行这没有什么发生,它不会抛出任何错误。
class employee(object):
def __init __(self,name,pay_rate,monday,tuesday ,星期三,星期四,星期五,星期六,星期日):
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\\\
本周您的工作时间为:,self.total_weekly_hours,\\\
def emp_name(self):
print\\\
当前雇员是:,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 munday hours)
self.tuesday = raw_input(星期四小时?)
self.wednesday = raw_input(wed hours?)
self.thursday = raw_input (星期五小时?)
self.saturday = raw_input(星期六小时?)
self.sunday = raw_input(星期几小时?)
self.object_name = raw_input命名对象)
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
/ pre>
def __init __(self,name,pay,hours):
#指定实例属性(不要调用from_input!)
def __str __ b $ b#替换emp_name,返回一个字符串
@property
def weekly_total(self):
return sum(self.hours)
@ class方法
def from_input(cls):
#take(and validate and convert!)input
return cls(name,pay,hours)
您可以使用:
employee = (John Smith,12.34,(8,8,8,8,8,0,0))
print str(employee)#call __str__
或:
employee = Employee.from_input $ b print employee.weekly_total#access property
请注意,不要为不同的日期分别分配实例属性,我假设每天的单个列表/元组的小时。如果日期名称很重要,请使用字典
{'Monday':7,...}
。记住,所有raw_input
是一个字符串,但你可能想要小时和支付为浮点数;有关输入验证的详情,请参见此处。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 errors either.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"
解决方案Your current code has a neverending loop in it, as
__init__
andcreate_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 allraw_input
is a string, but you probably want hours and pay as floats; for more on input validation, see here.这篇关于尝试通过用户的输入创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!