【问题标题】:Python List not appending value to list when using function使用函数时,Python List 未将值附加到列表
【发布时间】:2021-08-18 03:12:29
【问题描述】:

我有一个名为 func() 的函数,它接受一个值 n 并将其转换为一个列表。每次运行该函数时,我都想将其附加到 qwert。

qwert 的期望输出是:[[3],[4],[5]]

我得到的是:[]

我无法弄清楚问题。


def func(n):
    global qwert
    qwe=[n]
    qwe.append(qwert)
    print('qwe=')
    print(qwe)
    print('\nqwert=')    
    print(qwert)
    return 1

func(3)
func(4)
func(5)

print (qwert)

【问题讨论】:

    标签: python list function


    【解决方案1】:

    在我看来这是一个超级愚蠢的错误:P

    def func(n):
        global qwert
        qwe=[n]
        qwert.append(qwe)
        print('qwe=')
        print(qwe)
        print('\nqwert=')    
        print(qwert)
        return 1
    func(3)
    func(4)
    func(5)
    print ('\n\nfinal output:')
    print (qwert)```
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2013-12-30
      • 2021-12-03
      • 1970-01-01
      • 2023-02-02
      相关资源
      最近更新 更多