Python 在函数中放置While循环

Python 在函数中放置While循环,python,list,function,while-loop,Python,List,Function,While Loop,我是编程新手,希望在函数中放置一个while循环,以便调用该函数。下面是我写的代码。循环本身工作正常,但当我尝试将列表“number”放入函数时,它永远不会被追加 numbers = [] def loop_function(numbers): x = 6 i = 0 while i < x: print "At the top i is %d" % i numbers.append(i) i = i + 1

我是编程新手,希望在函数中放置一个while循环,以便调用该函数。下面是我写的代码。循环本身工作正常,但当我尝试将列表“number”放入函数时,它永远不会被追加

numbers = []

def loop_function(numbers):
    x = 6
    i = 0
    while i < x:
        print "At the top i is %d" % i
        numbers.append(i)

        i = i + 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d\n" % i

    return numbers

print "The numbers: " 

for num in numbers:
    print num
number=[]
def loop_功能(编号):
x=6
i=0
而i
您有函数定义,不需要调用它

numbers = []

def loop_function(numbers):
    x = 6
    i = 0
    while i < x:
        print "At the top i is %d" % i
        numbers.append(i)

        i = i + 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d\n" % i

    return numbers

loop_function(numbers)
print "The numbers: " 

for num in numbers:
    print num

好的,要打印函数,我需要添加一行代码,它有:print loop_function()?你的意思是“打印函数”。要打印函数返回的值?对但全局变量和局部变量使用相同的名称,它们通过引用传递,因此是相同的变量。这可不是什么好主意。为functionOh中的变量选择另一个名称!所以,我添加了那行代码,并在括号中加上“数字”,它就运行了!我将局部变量更改为“list”。这会使它成为更好的代码吗?听起来是个好主意。我会记住不要使用关键字作为变量名。
def loop_function(num):
    num.extend(range(6))