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

Python 我的程序应该读取用户选择的文件的内容,并调用函数draw_graph来绘制图形,python,tuples,Python,Tuples,我的程序不打印打印(“{:5d}”。格式化(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

我的程序不打印
打印(“{:5d}”。格式化(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()       
导入pylab
def绘制图形(x,y):
''x图与y图(相同长度的数字列表)''
#图形标题和轴标签
pylab.标题(“全球平均温度的变化”)
pylab.xlabel(“年度”)
pylab.ylabel(“温度偏差”)
#创建并显示绘图
派拉布图(x,y)
pylab.show()
def open_文件(x):
气候=开放(x,“r”)
回归气候
def过程_文件(气候):
file=input(“输入输出文件的名称:”)
输出文件=打开(文件“w”)
tup_list=[]
对于气候中的线路:
field=line.split()
年份=字段[0]
温度偏差=场[1]
记录=(年份、温度和偏差)
tup_list.append(记录)
回归气候
输出_文件。关闭()
打印(“{:6s}”。格式(“年份”,“温度偏差”))
打印(30*'-')
对于范围内的i(行):
x=图组列表[i][0]
y=tup_列表[i][1]
打印(“{:5d}”。格式(x,y))
绘制图(x,y)
def main():
filename=输入(“输入文件名:”)
尝试:
f=打开文件(文件名)
除FileNotFoundError外:
打印(“文件名无效。请重试。”)
进程文件(f)
f、 关闭()
main()

之前有一个return语句,它会使“不可见”后面的所有代码……

我的程序不会打印“print({:5d}).format(x,y))”或绘制图形??谢谢,我是python新手,所以我没有意识到这一点。。我是否将return气候放在draw_图(x,y)之后?与许多(任何?)语言一样,return语句终止函数调用。在您的例子中,似乎您返回了气候,但从未使用该返回值,所以只需删除该行即可。如果您确实需要返回值,请确保将所有需要执行的代码的其余部分放在前面,这样就可以了。