简体   繁体   English

**更新** Python 基于用户定义的方法编程

[英]**UPDATED** Python Programming based on user-defined methods

Can someone help me with the correct code I should use please?有人可以帮助我使用我应该使用的正确代码吗? I still haven't figured it out.我还没有弄明白。


I've posted the code below and the error outcome I'm receiving below.我已经发布了下面的代码以及我在下面收到的错误结果。 Some ages are being reported incorrectly.有些年龄报告不正确。 Can someone help me with the correct code please?有人可以帮我提供正确的代码吗? Thank you for your time and any assistance provided.感谢您的时间和提供的任何帮助。

Someone below helped me figure out their birthdates.下面有人帮我算出他们的生日。

(Sarah = 2000/08/01, Eric = 2009/08/02, Carter = 2009/07/28, Georgia = 2005/09/01) (莎拉 = 2000/08/01,埃里克 = 2009/08/02,卡特 = 2009/07/28,乔治亚 = 2005/09/01)


This is the code:这是代码:

from datetime import date

class Person:
  def __init__(self, name, birthdate):
    self.name = name
    self.birthdate = birthdate
  


def get_age(self):
    birthdate = self.birthdate
    today = date.today()
    if today.month >= birthdate.month and today.day >= birthdate.day:
        self.age = (today.year - birthdate.year)
    else:
        self.age = (today.year - birthdate.year) - 1
    return self.age

This is the outcome (some of the age tests are failing):这是结果(一些年龄测试失败):

#TEST 1#
sara.get_name() returned Sara
inputs:

outputs:
----------
#TEST 2#
sara.get_height() returned 160
inputs:

outputs:
----------
#TEST 3#
sara.get_age() returned 20
inputs:

outputs:
----------
#TEST 4#
sara.get_description() returned Sara is 160 cm high and is 20 years old.
inputs:

outputs:
----------
#TEST 5#
eric.get_age() returned 10
inputs:

outputs:
----------
#TEST 6#
** ERROR **carter.get_age() returned 10
* EXPECTED * 11
inputs:

outputs:
----------
#TEST 7#
georgia.get_age() returned 14
inputs:

outputs:

I think you have a logic error in get_age() :我认为您在get_age()中存在逻辑错误:

  def get_age(self):
    birthdate = self.birthdate
    today = date.today()
    if today.month >= birthdate.month and today.day >= birthdate.day:
      self.age = today.year - birthdate.year
    else:
      # haven't reached birthday yet
      self.age = (today.year - birthdate.year) - 1
    return self.age

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM