当前位置: 代码迷 >> python >> 我的程序应该读取用户选择的文件的内容,并将调用函数draw_graph绘制图形
  详细解决方案

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

热度:69   发布时间:2023-06-13 15:16:14.0

我的程序不打印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()       

您之前有一个return语句,这使得所有代码在“不可见”之后...

  相关解决方案