2

我目前正在完成一项涵盖定义功能的练习,并且在将所有支持功能放入主功能方面遇到了障碍。

这是代码最初的样子..

fn = input('Enter filename: ')
f = open(fn)
f.readline()  # Skip the first two lines
f.readline()
line = f.readline()  # Line containing site info
stuff = line.split(',')
print('\nSite: {}.\n(lat, long) = ({}, {})'.format(stuff[0], stuff[3], stuff[4]))

# Now process body of file, accumulating monthly rainfalls.

f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()

line = f.readline()
stuff = line.split(',')
rfs = 12 * [0]  # Rainfall totals for months
while len(stuff) > 1:
    date = stuff[1]
    m = int(date[4:6])   # Month
    rainfall = float(stuff[2])
    rfs[m - 1] += rainfall
    line = f.readline()
    stuff = line.split(',')

# Print results

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
print('\nMonth  Rainfall')
for i in range(12):
    print(' {} {:6.1f}'.format(months[i], rfs[i]))

print('\nTotal rainfall = {:.1f}'.format(sum(rfs)))
f.close()

这打印出来..

Enter filename:
Site: Christchurch Aero.
(lat, long) = (-43.493, 172.537)

Month  Rainfall
 Jan   58.0
 Feb   35.4
 Mar   54.2
 Apr   50.8
 May   52.6
 Jun   41.0
 Jul   29.4
 Aug   62.6
 Sep   21.4
 Oct   90.8
 Nov   63.6
 Dec   60.8

Total rainfall = 620.6

我现在尝试将此代码拆分为单独的函数,但每当我运行代码时,它就会冻结,我不确定潜在的问题是什么。这是我所拥有的..

def main():
    '''Main function, opens the text file, reads and then prints the
    relevant info'''
    file_input = input('Enter filename: ')
    file = open(file_input)

    file.readline()
    file.readline()

    site_line = file.readline()  # Line containing site info
    site_info = site_line.split(',')
    print('\nSite: {}.\n(lat, long) = ({}, {})'.
          format(site_info[0], site_info[3], site_info[4]))

    rfs = rainfall_function(file)

    print_rainfall(rfs)

def rainfall_function(file):
    '''Read text file for the required data and process the information'''   
    file.readline()
    file.readline()
    file.readline()
    file.readline()
    file.readline()
    file.readline()  

    rain_line = file.readline()
    rain_info = rain_line.split(',')
    rfs = 12 * [0]  # Rainfall totals for months
    while len(rain_info) > 1:
        date = rain_info[1]
        month = int(date[4:6])   # Month
        rainfall = float(rain_info[2])
        rfs[month - 1] += rainfall
    return rfs


def print_rainfall(rfs):
    '''Months to print rainfall statistics for'''
    months = ['Jan', 'Feb', 'Mar', 'Apr', ' May', 'Jun', 
              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    print('\nMonth  Rainfall')

    for i in range(12):
        print(' {} {:6.1f}'.format(months[i], rfs[i]))

    print('\nTotal rainfall = {:.1f}'.format(sum(rfs)))     

main()

我很感激帮助!

4

1 回答 1

5

我没有尝试运行你的代码,但你有一个明显的无限循环:

while len(rain_info) > 1:
        date = rain_info[1]
        month = int(date[4:6])   # Month
        rainfall = float(rain_info[2])
        rfs[month - 1] += rainfall

只要循环len(rain_info) > 1内没有任何内容改变rain_info. 所以,如果这个循环曾经进入过,它就永远不会再离开了。

print()插入语句以查看您的代码在做什么通常很有用。如果您在此循环之前和之后进行了打印,您将看到之前的打印而看不到之后的打印。如果您在循环中有打印,您会看到它重复。

单步执行代码也很有用。Python 有几个调试器可用;我使用的那个是 Wingware IDE 中的那个。有一个免费的版本供学生使用。

编辑:查看您的原始代码,我明白为什么它没有无限循环。它包括以下几行:

while len(stuff) > 1:
    # ...lines omitted...
    line = f.readline()
    stuff = line.split(',')

所以每次循环运行时,它都会尝试读取另一个输入行,然后拆分它得到的行。由于原始的 while 循环依赖于len(stuff),因此循环可以由于循环内的代码而终止。

于 2013-05-10T02:33:05.843 回答