我想把它分成几个更小的函数,以使它看起来更整洁,更容易阅读,但是每当我尝试这个时,我似乎都无法让它工作。任何帮助,将不胜感激。
目前我的代码看起来像这样
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])
total_rainfall = 0
for col in columns[2 : 2 + num_days]:
total_rainfall += float(col)
print('Month {:2}: {:.1f}'.format(month, total_rainfall))
我希望它看起来更像下面的示例
def print_monthly_totals (input_csv_filename):
data = open(input_csv_filename).readlines()
print('Rainfall totals for each month')
def SOMETHING(SOMETHING): #The SOMETHING is just a filler
for line in data:
columns = line.split(',')
month = int(columns[0])
num_days = int(columns[1])
total_rainfall = 0
def SOMETHING(SOMETHING): #The SOMETHING is just a filler
for col in columns[2 : 2 + num_days]:
total_rainfall += float(col)
print('Month {:2}: {:.1f}'.format(month, total_rainfall))