[英]Python 2.7 classes, user input birthdates for later use
"The class should initialize with a name and a birthday, but the birthday should be None." “该类应使用名称和生日来初始化,但生日应为“无”。 "There should be two methods, name and birthday" "setBirthday sets their Birthday to a date" these are the instructions I was given, this is a small piece in the larger picture.
“应该有两种方法,名称和生日”“ setBirthday将其生日设置为日期”,这是我得到的指示,这是大图中的一小部分。
I'm trying to set the day, the month and year to be entered in by the user...this will be used later for other calculations.. 我正在尝试设置要由用户输入的日期,月份和年份...这将在以后用于其他计算。
Code: 码:
class person(object):
def __init__(self, name):
self.name = name
self.setBirthday = None
#getName returns the name of the person
def getName(self):
return self.name
#setBirthday sets their Birthday to a date
def setBirthday(self):
day = (raw_input('Please enter the date of the month you were born on here ->'))
return self.day
month = (raw_input('Please enter the month of the year you were born on here ->'))
return self.month
year = (raw_input('Please enter the year you were born on here ->'))
return self.year
varName = person(raw_input('Please enter your name here ->'))
varDate = person.setBirthday()
Can you point me in the right direction? 你能为我指出正确的方向吗? This class stuff really confuses me...I need to have the user be able to input the day, month and year to be saved for later use.
这个类的东西确实让我感到困惑...我需要让用户能够输入要保存以供以后使用的日期,月份和年份。 error is below in comments.
错误在注释中。 I did remove the returns in my code.
我确实删除了代码中的返回值。
setBirthday
as a method, but as soon as you instantiate the class that method disappears and setBirthday
just points to None
. setBirthday
定义为方法,但是实例化该类后,该方法就会消失,并且setBirthday
只是指向None
。 Note that birthday
and setBirthday
are two unique names, and that class is already using the latter for that method. birthday
和setBirthday
是两个唯一的名称,以及类已经使用后者的这种方法。 var = (input())
is not such an occasion. var = (input())
并非这种情况。 @property
decorator). @property
装饰器标记的方法)。 MyClass.methodname()
, you need to pass it an instance of the class. MyClass.methodname()
,您需要向其传递该类的实例。 This is why standard Python style (PEP-8) calls for class names to be Uppercase
or UpperCase
and other objects to be lowercase
or snake_case
. Uppercase
或UpperCase
而其他对象为lowercase
或snake_case
。 Naming the class person
made it look like an instance of something when it's actually a class, making it all too easy to misuse it. person
命名实际上使它看起来像是某个事物的实例,因此很容易滥用它。 Here is what the code should look like: 代码如下所示:
class Person(object):
def __init__(self, name):
self.name = name
self.birthday = None
def getName(self):
'''return the name of the person'''
# this method should be removed. to get a person's name,
# use "person.name" instead of "person.getName()"
return self.name
def setBirthday(self):
'''set their Birthday to a date'''
day = raw_input('Please enter the date of the month you were born on here ->')
month = raw_input('Please enter the month of the year you were born on here ->')
year = raw_input('Please enter the year you were born on here ->')
self.birthday = int(day), int(month), int(year)
person = Person(raw_input('Please enter your name here ->'))
person.setBirthday()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.