1

我现在有一个程序,我正试图将其分解成更小、更易于管理的部分以用于锻炼目的。但问题是,我在遵循指导方针时遇到了一些困难。值得注意的是,一个函数不能包含超过 6 个语句。

这是程序最初的样子..

def print_monthly_totals (input_csv_filename):
'''Read the given CSV file and print the totals for each month.
   The input file is assumed to have the month number in column 1,
   the number of days in the month in column 2 and the floating
   point rainfalls (in mm) for each month in the remaining columns
   of the row. 
'''
    data = open(input_csv_filename).readlines()
    print('Rainfall totals for each month')
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])
        total_rainfall = 0
        for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)
        print('Month {:2}: {:.1f}'.format(month, total_rainfall))

print_monthly_totals('rainfalls2011.csv')

我设法把它分解成两个独立的功能给我..

def print_monthly_totals(input_csv_filename):
    data = open(input_csv_filename).readlines()
    print('Rainfall totals for each month')
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])     
        trf = rainfall_total(columns, num_days)  
        print('Month {:2}: {:.1f}'.format(month, trf))  

def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall

print_monthly_totals('rainfalls2011.csv')

但是我的print_monthly_totals函数仍然太大,我需要创建第三个函数来满足每个函数规则的 6 个语句。我刚刚导致一系列全局名称的尝试未定义错误..

def print_monthly_totals(input_csv_filename):
    data = open(input_csv_filename).readlines()      
    print('Rainfall totals for each month')
    trf = rainfall_total(columns, num_days)        
    print('Month {:2}: {:.1f}'.format(month, trf))      

def data():
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])         

def rainfall_total(columns, num_days):
    '''Guts of the program'''
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall

print_monthly_totals('rainfalls2011.csv')

任何帮助将不胜感激,干杯!

4

3 回答 3

1
def parse_line(data):
    columns = data.split(',')
    month = int(columns[0])
    num_days = int(columns[1])
    return month, num_days

def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall

def print_monthly_totals(input_csv_filename):
    with open(input_csv_filename) as fh:
        for line in fh:
            month, days = parse_line(line)
            print('Rainfall totals for each month')
            trf = rainfall_total(columns, num_days)        
            print('Month {:2}: {:.1f}'.format(month, trf))      

print_monthly_totals('rainfalls2011.csv')

可能是这样的。显然这可能行不通..有点乱,但我会继续编辑它,希望你能明白。

好处:

  • 没有打开的文件句柄
  • 调用顺序中的函数
  • 删除了过多的代码
于 2013-05-07T09:41:44.377 回答
1

这个怎么样:

def print_monthly_totals(input_csv_filename):
    with open(input_csv_filename) as csv_file:
        data = csv_file.readlines()
        print('Rainfall totals for each month')
        for line in data:
            manage_line(line)


def manage_line(line):
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])     
        trf = rainfall_total(columns, num_days)  
        print('Month {:2}: {:.1f}'.format(month, trf))  

def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall

print_monthly_totals('rainfalls2011.csv')

这听起来非常接近您可能想要的。希望能帮助到你。

于 2013-05-07T09:42:16.637 回答
0

这张照片还展示了如何使用该csv模块:

def print_monthly_totals (input_csv_filename):
    '''
    Read the given CSV file and print the totals for each month.
    The input file is assumed to have the month number in column 1,
    the number of days in the month in column 2 and the floating
    point rainfalls (in mm) for each month in the remaining columns
    of the row. 
    '''    
    import csv
    print('Rainfall totals for each month')
    with open(input_csv_filename) as csv_file:
        for row in csv.reader(csv_file):
            print_row(row)

def print_row(columns):
    month = int(columns[0])
    num_days = int(columns[1])
    total_rainfall = sum(float(val) for val in columns[2:2+num_days])
    print('Month {:2}: {:.1f}'.format(month, total_rainfall))

print_monthly_totals('rainfalls2011.csv')
于 2013-05-07T10:09:07.763 回答