Python 从While循环结构到函数调用的转换

Python 从While循环结构到函数调用的转换,python,Python,我正在努力学习Python的一个例子--Python--我只是想不通。我想我在改变程序的理论因素上打嗝,而不是写物理行,但我可能完全错了 i = 0 numbers =[] while i < 6: print "At the top i is %d" %i numbers.append(i) i += 1 print "Numbers now: ", numbers print "At the bottom i is %d" %i print

我正在努力学习Python的一个例子--Python--我只是想不通。我想我在改变程序的理论因素上打嗝,而不是写物理行,但我可能完全错了

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

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

print "The numbers: "
for num in numbers:
    print num
输出:

At the top i is 0
numbers now:  [0]
At the top i is 1
numbers now:  [0, 1]
At the top i is 2
numbers now:  [0, 1, 2]
At the top i is 3
numbers now:  [0, 1, 2, 3]
At the top i is 4
numbers now:  [0, 1, 2, 3, 4]
At the top i is 5
numbers now:  [0, 1, 2, 3, 4, 5]
The numbers: 
0
1
2
3
4
5
输出:

At the top i is 0
numbers now:  [0]
At the top i is 1
numbers now:  [0, 1]
At the top i is 2
numbers now:  [0, 1, 2]
At the top i is 3
numbers now:  [0, 1, 2, 3]
At the top i is 4
numbers now:  [0, 1, 2, 3, 4]
At the top i is 5
numbers now:  [0, 1, 2, 3, 4, 5]
The numbers: 
0
1
2
3
4
5

它要求您用函数调用来模拟while循环。您只需要递归地调用函数本身

def f(i,x,numbers):
    if (i < x):
        print "At the top i is %d" %i
        numbers.append(i)

        i += 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" %i
        f(i,x,numbers)
    return numbers

numbers = f(0,6,[])
for num in numbers:
    print num

它要求您用函数调用来模拟while循环。您只需要递归地调用函数本身

def f(i,x,numbers):
    if (i < x):
        print "At the top i is %d" %i
        numbers.append(i)

        i += 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" %i
        f(i,x,numbers)
    return numbers

numbers = f(0,6,[])
for num in numbers:
    print num

这个问题我来晚了,但我也在上这门课,我想我可以加入讨论。我认为下面的代码正确地回答了本课的1-3个学习练习

    numbers = []
    def numbers_list(x,y):
        i = 0
        while i < x:
            print "At the top of i is %d" % i
            numbers.append(i)
            i += y
            print "Numbers now: ", numbers
            print "At the bottom of i is %d" % i
        return numbers

    numbers_list(6,1)
    print "the numbers: "

    for num in numbers:
        print num
number=[]
def编号列表(x,y):
i=0
而i

输出将与上面相同。

这个问题我来晚了,但我也在上这门课,我想我可以加入讨论。我认为下面的代码正确地回答了本课的1-3个学习练习

    numbers = []
    def numbers_list(x,y):
        i = 0
        while i < x:
            print "At the top of i is %d" % i
            numbers.append(i)
            i += y
            print "Numbers now: ", numbers
            print "At the bottom of i is %d" % i
        return numbers

    numbers_list(6,1)
    print "the numbers: "

    for num in numbers:
        print num
number=[]
def编号列表(x,y):
i=0
而i

输出将与上面相同。

上面由black_bird提出的最初问题来自Zed Shaw

在ex33 SD1中,Shaw要求您以练习中的while循环为例,将其转换为函数,将硬编码的循环条件替换为变量。虽然上面的一些答案达到了目标,但没有一个符合Shaw概述的说明,该说明只需要通过一个参数(上面的最后一个答案很接近,但通过了两个)。下面是两个答案。第一个答案是与Shaw在ex33中的研究练习1一致的最佳答案,与读者在这一点上所学到的内容一致。只传递一个参数。第二个函数获取用户输入,而不是在函数调用中硬编码数字

第一个答案:

def buildList(num):
    numList = []    
    i = 0       
    while i < num:
        numList.append(i)
        i += 1
        print "i: %d, " % i,
        print "list: ", numList
    #note: you don't need to return a value

#you can hard code any number here, of course
buildList(6)
def构建列表(num):
numList=[]
i=0
而我
下面是第二个答案:

def buildList(num):
    #Convert num to int else infinite loop because num is a string
    num = int(num)  
    numList = []    
    i = 0       
    while i < num:
        numList.append(i)
        i += 1
        print "i: %d, " % i,
        print "list: ", numList

#getting/passing a string; converted to int within the function
answer = raw_input("Enter a number less than 10: ")
buildList(answer)
def构建列表(num):
#将num转换为int-else无限循环,因为num是字符串
num=int(num)
numList=[]
i=0
而我
上面由black_bird提出的最初问题来自Zed Shaw

在ex33 SD1中,Shaw要求您以练习中的while循环为例,将其转换为函数,将硬编码的循环条件替换为变量。虽然上面的一些答案达到了目标,但没有一个符合Shaw概述的说明,该说明只需要通过一个参数(上面的最后一个答案很接近,但通过了两个)。下面是两个答案。第一个答案是与Shaw在ex33中的研究练习1一致的最佳答案,与读者在这一点上所学到的内容一致。只传递一个参数。第二个函数获取用户输入,而不是在函数调用中硬编码数字

第一个答案:

def buildList(num):
    numList = []    
    i = 0       
    while i < num:
        numList.append(i)
        i += 1
        print "i: %d, " % i,
        print "list: ", numList
    #note: you don't need to return a value

#you can hard code any number here, of course
buildList(6)
def构建列表(num):
numList=[]
i=0
而我
下面是第二个答案:

def buildList(num):
    #Convert num to int else infinite loop because num is a string
    num = int(num)  
    numList = []    
    i = 0       
    while i < num:
        numList.append(i)
        i += 1
        print "i: %d, " % i,
        print "list: ", numList

#getting/passing a string; converted to int within the function
answer = raw_input("Enter a number less than 10: ")
buildList(answer)
def构建列表(num):
#将num转换为int-else无限循环,因为num是字符串
num=int(num)
numList=[]
i=0
而我
def while_循环(数字,增量):
i=0
数字=[]
而我<数字:
打印“顶部i为%d”%i
数字。附加(i)
i+=增量
打印“立即打印数字:”,数字
在底部打印“i为%d”%i
打印“数字”:
对于数字中的num:
打印数
while_循环(int(原始输入(“输入一个数字”)),int(原始输入(“输入增量”))
def while_循环(数字,增量):
i=0
数字=[]
而我<数字:
打印“顶部i为%d”%i
数字。附加(i)
i+=增量
打印“立即打印数字:”,数字
在底部打印“i为%d”%i
打印“数字”:
对于数字中的num:
打印数
while_循环(int(原始输入(“输入一个数字”)),int(原始输入(“输入增量”))

我想Zed Shaw只是想让我们将while循环(而不是for循环)转换成函数。。。所以我觉得就这么简单

def functiontocallforlist(x):
numList = []
i = 0
while i < x:
    print "At the top i is %d" % i
    numList.append(i)

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

#You can put whatever number you want as x really.
functiontocallforlist(8)
def函数调用列表(x):
numList=[]
i=0
而i# get the input from the user for i
i = int(input("i>"))
# create an empty list and fill it later
numbers = []
# get input from user for the variable to use in while
num = int(input("num>"))
# Create a function and use while loop
def yourfunction(i):
    while i < num:
        # if user input for "i" is less than the user input for "num"
        print(f"at the top of i is {i}")
        # once i is printed, Add i to the list
        numbers.append(i)
        #once i is added to the list, print the list
        print("numbers now", numbers)
        # Then increment i by 1
        i = i + 1
        #once incremented, print i and repeat
        print(f"At the bottom of i is {i}")
# Call the function
yourfunction(i)
# print the newly created list
print(f"the new list is {numbers}")
# use of for loop 
for num in numbers:
    print(num)][1]
def function_as_while(length):
numbers = []
for i in range(length):
    print(f"At the top i is {i}")
    numbers.append(i)
    print(f"numbers now: {numbers}")

return numbers
numbers = function_as_while(int(input("Enter list length:  ")))
print("The numbers: ")
for number in numbers:
    print(number)