【问题标题】:I can't understand exactly why list is not created after successfully executed also?我不明白为什么在成功执行后也没有创建列表?
【发布时间】:2017-04-18 14:16:24
【问题描述】:

当我在 IDE 中运行时,创建了列表但列表为空,我该如何解决这个问题?

>>> def findnum(n,m):
          print('hellow')
          l=list(range(n,m))
>>> findnum(1,10)
hellow
>>> l
[]

【问题讨论】:

  • 你没有从函数中返回任何东西,所以lfindnum() 的本地...尝试:l = findnum(1, 10)findnum()return l 结尾。这是初级编程,因此您可能需要阅读更多介绍性材料。
  • 这是否与单独的文件写入相同,我的意思是在键入并保存在 findnum.py 之类的文件中时

标签: python python-3.x list function


【解决方案1】:

从函数返回列表并将其放入单独的变量中。这里, l 是仅在函数范围内具有数据的列表。以下代码 sn-p 可能会有所帮助:

>>> def findnum(n,m):
      print('hellow')
      l=list(range(n,m))
      return l
>>> l = findnum(1,10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多