在python中将列表作为参数传递时发生未定义的错误 [英] undefined error when passing list as parameter in python

查看:113
本文介绍了在python中将列表作为参数传递时发生未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我以前有代码设置来访问在函数外部声明的列表,从而使其成为全局变量,对于分配给我的任务,讲师坚持认为我们不使用全局变量.所以现在我有了以下代码,显然给出了"data_set undefined error"

So I previously had my code set-up to access a list that was declared outside functions making it a global variable, for the task I was assigned the lecturer is insisting that we do not use global variables. So now I have the following code which obviously gives a "data_set undefined error"

这样的想法是,我从一个csv文件创建一个列表,然后将该列表传递给其他几个函数,这样我就可以从data_set列表中获取元素并创建新的输出.

The idea being that I create a list from a csv file and then pass that list to several other functions so that I can get elements from the data_set list and create new outputs.

import csv
import sys

def print_menu():
    print("ACME WEATHER DATA APP")
    print("1) Choose weather data file")
    print("2) See data for selected day")
    print("3) Calculate average statistics for the data")
    print("4) Print a scatterplot of the average temperatures")
    print("0) Quit program")

def loadFile(x):
    data_set = []
    data_set.clear()
    with open(x, "r") as readfile:
        csv_reader = csv.reader(readfile, delimiter= ';')
        for row in csv_reader:
            data_set.append(row)
    print("Loaded weather data from", (x[0:-4]).capitalize())
    print()
    return data_set    

def avgDay(x, data_set):
    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
           print("The weather on", x, "was on average", line[2], "centigrade")


def lowHigh(x, data_set):
    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
            print("The lowest temperature was", line[3], "and the highest temperature was", line[4])


def rain(x, data_set):
    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
            print("There was", line[1],"mm rain")
            print()

def avgMonth(data_set):
    avgList= []
    for line in data[1:]:
        avgList.append(line[2])
    avgList= [float(i) for i in avgList]
    avg = sum(avgList)/len(avgList)
    print("The average temperature for the 25 day period was", round(avg,1))


def avgLow(data_set):
    avgList= []
    for line in data[1:]:
        avgList.append(line[3]) 
    avgList= [float(i) for i in avgList]
    avg = sum(avgList)/len(avgList)
    print("The average lowest temperature was", round(avg,1))


def avgHigh(data_set):
    avgList= []
    for line in data[1:]:
        avgList.append(line[4])  
    avgList= [float(i) for i in avgList]
    avg = sum(avgList)/len(avgList)

    print("The average highest temperature was", round(avg,1))
    print()

def print_tempLine(day, month, temp):
    print(day + "." + month + " ", end="")
    print("   "*(temp+5) + "-", end="")
    print()


def print_tempAxis():
    print("      ", end="")
    for i in range(-5,16):
        print("{:02d} ".format(i), end="")
    print()

def scatPlot(data_set):
    for line in data[1:]:
            day_month=line[0].split("-")
            temp=int(round(float(line[2]),0))
            print_tempLine(day_month[2],day_month[1],temp)
            print_tempAxis()
            print()

def menu_number():
    number = int(input("Choose what to do: "))
    if (number) == 1:
        x = input("Give name of the file: " )
        loadFile(x)
        print_menu()
        menu_number()

    elif (number) == 2:
        x = input("Give a date (dd.mm): ")
        avgDay(x, data_set)
        lowHigh(x, data_set)
        rain(x, data_set)
        print_menu()
        menu_number()

    elif (number) == 3:
        avgMonth(data_set)
        avgLow(data_set)
        avgHigh(data_set)
        print_menu()
        menu_number()

    elif (number) == 4:
        scatPlot(data_set)
        print_menu()
        menu_number()

    elif (number) == 0:
        sys.exit()

    else:
        print("\n Your selection is invalid, please try again!\n")
        print_menu()
        menu_number()

print_menu()
menu_number()

我了解到,当我在loadFile(x)中返回data_set时,需要以某种方式将其分配给变量,以便可以在本地范围之外对其进行访问,我只是不确定如何做到这一点.我尝试在menu_number函数中将loadFile(x)更改为data_set = loadFile(x),这只是导致它给出以下错误分配前引用了本地变量'data_set'".那么,有什么我想念的东西吗?还是需要以某种方式重新编写代码?

I understand that when I return data_set in my loadFile(x) that it needs to be assigned to a variable somehow so that it can be accessed outside the local scope, I just am not sure how that is done. I have tried changing loadFile(x) to data_set = loadFile(x) within menu_number function, this just resulted in it giving the following error "local variable 'data_set' referenced before assignment". So is there something I have missed or do I need to re-write the code somehow?

对不起,我没有意识到我需要提供完整的堆栈跟踪错误

Sorry did not realize I needed to provide full stack trace error

Traceback (most recent call last):
  File "C:\Users\alexa\Documents\XAMK\python scripts\test.py", line 126, in <module>
    menu_number()
  File "C:\Users\alexa\Documents\XAMK\python scripts\test.py", line 95, in menu_number
    menu_number()
  File "C:\Users\alexa\Documents\XAMK\python scripts\test.py", line 99, in menu_number
    avgDay(x, data_set)
NameError: name 'data_set' is not defined

csv示例代码

"DateTime";"Precipitation";"Mean temperature";"Minimum temperature";"Maximum temperature";"Typical maximum temperature";"Typical maximum temperature";"Typical minimum temperature";"Typical minimum temperature";"Fairly typical maximum temperature";"Fairly typical maximum temperature";"Fairly typical minimum temperature";"Fairly typical minimum temperature"
"2019-10-06";0;4;0.4;6.8;10.3;11.9;4.5;7.2;7.6;13.8;0.9;9.8
"2019-10-07";0.1;4.4;2.6;7.8;10.1;11.8;4.4;7;7.4;13.6;0.8;9.7
"2019-10-08";0;4.9;2.3;7.2;10;11.6;4.2;6.9;7.2;13.4;0.6;9.5
"2019-10-09";1.1;4.8;1.8;7.6;9.8;11.4;4.1;6.7;6.9;13.3;0.5;9.4

推荐答案

如果您可以提供一个示例csv文件,以便我测试工作流程,将很有帮助.但是看起来问题是您调用了loadFile(x),并且它具有return语句,但是该return语句未设置任何内容.因此,使用data_set = loadFile(x)可以使您处于正确的轨道.问题是,当您再次调用menu_number函数时,变量值不会保留.因此,在第二次通话中,data_set失去了意义.

It would be helpful if you can provide an example csv file so that I can test the workflow. But it looks like the problem is that you call loadFile(x), and it has a return statement, but nothing is set to that return statement. So you are on the right track with data_set = loadFile(x). The problem is, when you call the menu_number function again, variable values are not preserved. So on that second call, data_set lost its meaning.

您可以通过将data_set的值传递给函数来解决此问题,因此您的menu_number看起来更像

You can fix this by passing the value of data_set to the function, so your menu_number would look more like

def menu_number(data_set=None):
    number = int(input("Choose what to do: "))
    if (number) == 1:
        x = input("Give name of the file: " )
        data_set=loadFile(x)
        print_menu()
        # passing data_set value along
        # with the call
        menu_number(data_set)

    elif (number) == 2:
        x = input("Give a date (dd.mm): ")
        avgDay(x, data_set)
        lowHigh(x, data_set)
        rain(x, data_set)
        print_menu()
        # passing data_set to preserve it
        menu_number(data_set)

    elif (number) == 3:
        avgMonth(data_set)
        avgLow(data_set)
        avgHigh(data_set)
        print_menu()
        # passing data_set to preserve it
        menu_number(data_set)

    elif (number) == 4:
        scatPlot(data_set)
        print_menu()
        # passing data_set to preserve it
        menu_number(data_set)

    elif (number) == 0:
        sys.exit()

    else:
        print("\n Your selection is invalid, please try again!\n")
        print_menu()
        # passing data_set to preserve it
        menu_number(data_set)

在此示例中,data_set是一个可选变量.如果调用menu_number(),则data_setNone.但是在调用menu_number(data_set)时,您可以重用该值.

In this example, data_set is an optional variable. If you call menu_number(), data_set is None. But on your calls of menu_number(data_set), you can reuse the value.

使data_set为可选参数将防止任何未定义的错误.将其传递回自身将避免您需要一遍又一遍地读取csv文件.

Making data_set an optional parameter will prevent any undefined errors. Passing it back to itself will prevent you from needing to read the csv file over and over again.

这篇关于在python中将列表作为参数传递时发生未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆