我目前正在进行一项涵盖定义功能的练习,并且在将所有支持功能放入主要功能方面遇到了麻烦。
以下是代码原来的样子..
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()
我很感激帮助!!
答案 0 :(得分: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)
,因此循环可以因循环内的代码而终止。