我的程序应该读取用户选择的文件的内容,并将调用函数draw_graph绘制图形



我的程序不打印print( "{:<20s} {:>5d}".format(x,y))或绘制图形?

import pylab
def draw_graph( x, y ):
    '''Plot x vs. y (lists of numbers of same length)'''
    # Title for the graph and labels for the axes
    pylab.title( "Change in Global Mean Temperature" )
    pylab.xlabel( "Year" )
    pylab.ylabel( "Temperature Deviation" )
    # Create and display the plot
    pylab.plot( x, y )
    pylab.show()

def open_file(x):
    climate = open(x, "r")
    return climate

def process_file(climate):
    file = input("Enter a name for the output file: ")
    output_file= open(file, "w")
    tup_list = []
    for line in climate:
        field = line.split()
        year = field[0]
        temperature_deviations = field[1] 
        record = ( year, temperature_deviations )
        tup_list.append( record )
    return climate
    output_file.close()
    print( "{:<10s}  {:>6s}".format( "Year" , "Temperature Deviation"))
    print(30*'-')
    for i in range(line):
        x = tup_list[i][0]
        y = tup_list[i][1]
        print( "{:<20s}  {:>5d}".format(x,y))
    draw_graph(x,y)
def main():
    filename = input("Enter a filename: ")
    try:
        f=open_file(filename)
    except FileNotFoundError:
        print("Invalid file name. Try Again.")
    process_file(f)
    f.close()
main()       

前面有一个返回语句,它使'不可见'之后的所有代码…

最新更新