You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Python中Region类get_number_of_births方法的逻辑错误?

修复Region类的get_number_of_births方法

原始代码

class Births:
    def __init__(self, year = 1990, number_of_births = 0):
        self.year = year
        self.number_of_births = number_of_births
        
    def __str__(self):
        return f'Births: {self.number_of_births} ({self.year})'
    
class Deaths:
    def __init__(self, year = 1990, number_of_deaths = 0):
        self.year = year
        self.number_of_deaths = number_of_deaths
    
    def __str__(self):
        return f'Deaths: {self.number_of_deaths} ({self.year})'
    
class Region:
    def __init__(self, name = 'Unknown', land_area = 1):
        self.name = name
        self.land_area = land_area
        self.births_list = []
        self.deaths_list = []
        
    def __str__(self):
        return f'{self.name} ({self.land_area} km^2)'
    
    def __eq__(self, other):
        if not isinstance(other, Region):
            return False
        return self.name == other.name
    
    def __lt__(self, other):
        if self.name < other.name:
            return True
        return False
    
    def add_birth_record(self, birth_record):
        self.births_list.append(birth_record)
        
    
    def add_death_record(self, death_record):
        self.deaths_list.append(death_record)
        
    
    def get_number_of_births(self, start_year, end_year):
        sum = 0
        for births_number in self.births_list:
            if births_number in range(start_year, end_year):
                sum += Region.add_birth_record
        return sum
            

region1 = Region("Bay of Plenty", 12072)
birth1 = Births(2018, 3969)
birth2 = Births(2019, 4047)
death1 = Deaths(2018, 2583)
death2 = Deaths(2019 ,2787)
region1.add_birth_record(birth1)
region1.add_birth_record(birth2)
print(region1.get_number_of_births(2018, 2018))
print(region1.get_number_of_births(2018, 2019))
print(region1.get_number_of_births(2018, 2020))
print(region1.get_number_of_births(2020, 2020))

问题分析

get_number_of_births方法存在三个核心错误:

  • 循环变量处理错误:遍历的是Births对象,却直接用对象与range比较,未访问对象的year属性
  • 区间判断逻辑错误:range(start_year, end_year)是左闭右开区间,无法满足“包含首尾年份”的需求
  • 累加逻辑错误:试图累加类方法Region.add_birth_record,而非Births对象的number_of_births属性

修复后的get_number_of_births方法

def get_number_of_births(self, start_year, end_year):
    total_births = 0
    for birth_record in self.births_list:
        # 直接判断年份是否在[start_year, end_year]闭区间内
        if start_year <= birth_record.year <= end_year:
            total_births += birth_record.number_of_births
    return total_births

修复后运行结果

执行原测试代码会输出:

3969
3969
8016
0

完整修复代码

class Births:
    def __init__(self, year = 1990, number_of_births = 0):
        self.year = year
        self.number_of_births = number_of_births
        
    def __str__(self):
        return f'Births: {self.number_of_births} ({self.year})'
    
class Deaths:
    def __init__(self, year = 1990, number_of_deaths = 0):
        self.year = year
        self.number_of_deaths = number_of_deaths
    
    def __str__(self):
        return f'Deaths: {self.number_of_deaths} ({self.year})'
    
class Region:
    def __init__(self, name = 'Unknown', land_area = 1):
        self.name = name
        self.land_area = land_area
        self.births_list = []
        self.deaths_list = []
        
    def __str__(self):
        return f'{self.name} ({self.land_area} km^2)'
    
    def __eq__(self, other):
        if not isinstance(other, Region):
            return False
        return self.name == other.name
    
    def __lt__(self, other):
        if self.name < other.name:
            return True
        return False
    
    def add_birth_record(self, birth_record):
        self.births_list.append(birth_record)
        
    
    def add_death_record(self, death_record):
        self.deaths_list.append(death_record)
        
    
    def get_number_of_births(self, start_year, end_year):
        total_births = 0
        for birth_record in self.births_list:
            if start_year <= birth_record.year <= end_year:
                total_births += birth_record.number_of_births
        return total_births
            

region1 = Region("Bay of Plenty", 12072)
birth1 = Births(2018, 3969)
birth2 = Births(2019, 4047)
death1 = Deaths(2018, 2583)
death2 = Deaths(2019 ,2787)
region1.add_birth_record(birth1)
region1.add_birth_record(birth2)
print(region1.get_number_of_births(2018, 2018))
print(region1.get_number_of_births(2018, 2019))
print(region1.get_number_of_births(2018, 2020))
print(region1.get_number_of_births(2020, 2020))

内容的提问来源于stack exchange,提问作者peterparker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:27:53