【问题标题】:print list error NameError: name 'lst1' is not defined [closed]打印列表错误NameError:名称'lst1'未定义[关闭]
【发布时间】:2014-10-06 14:58:37
【问题描述】:

当我尝试获取 lst1 (print(lst1)) 时遇到此代码问题,我收到此错误 NameError: name 'lst1' is not defined

我尝试了所有方法,但无法解决错误

# bkm( [500, 1024, 2000, 1100000] ) <- input
# ['0MB 0KB 500B', '0MB 1KB 0B', '0MB 1KB 976B', '1MB 50KB 224B'] <-this should be the output


    def bkm(lst):
        lst1=[]
        for i in lst:
            mb=i/1048576
            i=i%1048576
            kb=i/1024
            b=i%1024
            lst1=lst1+[str(mb)+'MB'+' '+str(kb)+'KB'+' '+str(b)+'B']
        return lst1
    bkm( [500, 1024, 2000, 1100000] )
    print (lst1)

【问题讨论】:

  • 您正试图访问函数外部的变量。

标签: python nameerror


【解决方案1】:

你需要得到返回的列表

my_list = bkm([500, 1024, 2000, 1100000])
print(my_list)

lst1bkm 函数完成后立即超出范围,因此您需要使用返回的列表并将其存储在新变量中。

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 2021-08-23
    • 2013-01-13
    • 1970-01-01
    • 2016-12-20
    • 2023-03-05
    • 2020-12-23
    • 2014-03-18
    • 2021-07-27
    相关资源
    最近更新 更多