从While循环结构转换为函数调用
我正在学习《Learn Python The Hard Way》这本书里的一个例子,但有些地方我想不明白。我觉得我在理解如何修改程序的理论部分上卡住了,而不是在写代码的具体行上,不过我也可能完全搞错了。
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
这是我正在处理的代码,问题/提示是这样的:
把这个while循环转换成一个可以调用的函数,并把测试中的6(i < 6)替换成一个变量。
如果你能帮我,并且顺便把每一行的意义用简单的语言解释一下,那对我会非常有帮助。
这是我第一次尝试:
def whilefunc(i):
numbers = []
for i in range(0,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
for num in numbers:
print num
9 个回答
0
def list_of_numbers(length):
working_list = []
for i in range(length):
print "At the top i is %d" %i
working_list.append(i)
print "numbers now: ", working_list
return working_list
numbers = list_of_numbers(6)
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
1
我觉得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)
2
我来得有点晚,不过我也在上这门课,觉得可以参与讨论。我认为下面的代码正确地回答了这节课的学习练习中的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
输出结果和上面的一样。
3
上面黑鸟提的初始问题是来自Zed Shaw的《Learn Python the Hard Way》中的第33章的学习练习1。你可以在这里找到这个练习。
在第33章的学习练习1中,Shaw让你把练习中的示例循环改成一个函数,并把固定的循环条件换成一个变量。虽然上面的一些回答达到了这个目标,但没有一个完全按照Shaw的要求来做,Shaw的要求是只传递一个参数(上面最后一个回答很接近,但传递了两个参数)。下面有两个回答。第一个是最符合Shaw的学习练习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 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)
1
这个问题让你用函数调用来模仿一个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
输出:
At the top i is 0
Numbers now: [0]
At the bottom, i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom, i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom, i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom, i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom, i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom, i is 6
0
1
2
3
4
5