3

我有一个关于循环的非常基本的问题。我正在尝试进入编程领域,目前正在阅读“Learn Python The Hard Way”一书,并被困在 ex 33 上的第 5 次学习练习中:http: //learnpythonthehardway.org/book/ex33.html

这就是我的代码的样子:

i = 0
numbers = []

def main(i, numbers):
    userloop = int(raw_input("Type in max value for the loop > "))
    while i < userloop:

        print "At the top i is %d" % i
        numbers.append(i)
        userincrease = int(raw_input("Increase > "))
        i += userincrease

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


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


main(i, numbers)

问题是我想使用 for 循环而不是 while 循环。这甚至可能,然后,我该怎么做?我试过简单地交换

while i < userloop:

for i in range(userloop)

但是,每次到达循环的“顶部”时, i 值都会重置。

由于我对此非常陌生,如果我错过了一些明显的东西,请不要太刻薄。

顺便问一下,有没有人知道这本书是不是很好,还是我在浪费时间?

4

5 回答 5

4

python 中的for循环遍历表达式 after 提供的一系列值in。在 Python 中,我们使用这个术语iterable来表示任何可以产生这样一个系列的东西。这可以是 a range(),但它可以是任何可以迭代(循环)的东西。循环变量i在每次迭代中接收下一个值,无论之前的值是什么。

因此,以下内容是合法的:

for i in ('foo', 'bar', 'baz'):
    print i
    i = 'spam'

i = 'spam'基本上什么都不做。

您必须以不同的方式处理它;跳过这些值:

print_at = 0
for i in range(userloop):
    if i >= print_at:
        print "At the top i is %d" % i
        numbers.append(i)
        userincrease = int(raw_input("Increase > "))
        print_at = i + userincrease

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

每次您要求用户增加时,我们将值更新为print_at当前值的总和i加上增加的值,然后继续忽略大部分循环体,直到i赶上print_at

于 2013-01-15T21:27:54.773 回答
2

for 循环和 while 循环之间有一个关键的区别。

for 循环:执行循环体固定次数,计算迭代次数。

while 循环:只要循环的条件为真,就重复执行循环体。

通常,当从零开始并停在特定数字(例如userloop)时,意味着 for 循环是最合适的。然而,这个程序并不是说“我要从 0 数到 userloop 并每次都做这些事情”;它的意思是“我从零开始,我将做这些事情,根据用户输入更改一个值,直到该值满足条件(> userloop)”。

用户可以多次输入 0,或者输入负数,你可能永远不会退出循环。

于 2013-01-15T21:31:06.453 回答
1

所以我检查了你链接到的 Python the Hard Way 网站上的问题......我认为他实际上只是要求你使用 for 循环和范围重写他的原始脚本,在这种情况下你可以做这样的事情:

numbers = []

for i in range(6):
    print "at the top i is", i
    numbers.append(i)
    print "numbers now", numbers

print "finally numbers is:"
for num in numbers:
    print num

如果你运行它,你会得到这样的输出:

$ python loops.py 
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]
finally numbers is:
0
1
2
3
4
5

要回答有关增量语句的问题,如果有必要,请尝试以下操作:

for i in range(6):
    print "at the top i is", i
    numbers.append(i)

    i += 2
    print "numbers now", numbers
    print "at the bottom i is", i

print "finally numbers is:"
for num in numbers:
    print num

你会得到这样的输出:

$ python loops.py
at the top i is 0
numbers now [0]
at the bottom i is 2
at the top i is 1
numbers now [0, 1]
at the bottom i is 3
at the top i is 2
numbers now [0, 1, 2]
at the bottom i is 4
at the top i is 3
numbers now [0, 1, 2, 3]
at the bottom i is 5
at the top i is 4
numbers now [0, 1, 2, 3, 4]
at the bottom i is 6
at the top i is 5
numbers now [0, 1, 2, 3, 4, 5]
at the bottom i is 7
finally numbers is:
0
1
2
3
4
5

你看到这里发生了什么吗?请记住,range 函数会生成这样的列表:
range(6) -> [0, 1, 2, 3, 4, 5]。
这是 Python 的 for 语句知道如何迭代的东西(他们称这些类型的东西为可迭代的)。无论如何,变量 i 每次迭代都绑定到 range(6) 中的一项(即列表 [0, 1, 2, 3, 4, 5])。即使你在 for 循环中对 i 做了什么,它也会被反弹到可迭代的下一个项目。所以这个增量语句绝对没有必要。在这种情况下,它只是将 2 添加到 i 中,但无论如何它都会像正常的每次迭代一样反弹。

好吧,毕竟,这是我在更改您的特定代码时使用的选项,每次将迭代器从使用 while 循环更改为使用带范围的 for 循环。

我认为您通过浏览您链接到的在线书籍的前几章已经涵盖了函数,但是如果您之前没有看过递归,它可能会有点令人困惑。再次调用函数之前的 if 语句是为了防止当前值超过范围内的值,这会给你一个关于超出最大递归深度的错误(python 这样做是为了防止堆栈溢出错误我很确定)。

最后一点,你可以用 range:
range(0, 10, 2) -> [0, 2, 4, 6, 8]来做这样的事情
,我在下面使用它:

def my_loop(max, current=0, increment=1, numbers=[]):
    for x in range(current, max, increment):
        print "at the top, current is %d" % current
        numbers.append(current)

        increment = int(raw_input("Increase > "))
        current += increment

        print "numbers is now: ", numbers
        print "at the bottom current is %d" % current
        break

    if current < max:
        my_loop(max, current, increment, numbers)
    else:
        print "no more iterating!"


max = int(raw_input("Type in max value for the loop > "))    

my_loop(max)

我不认为我会真的这样做,但至少这是一个有趣的尝试。

于 2013-01-16T02:05:43.703 回答
1

我是使用这段代码完成的,其中 p 是您在函数调用 numba(p) 中设置的参数

i = 0
elements = [ ]

def numba(p):
    for i in range(0, p):
        print "At the top i is %r" % i
        elements.append(i)
        print "Numbers now: ", elements
        print "At the bottom i is %r" % (i + 1)

numba(3)
于 2013-12-02T04:48:18.553 回答
0

好吧,我想你可以尝试挖掘动态控制循环的生成器的方向for

于 2013-01-15T22:16:34.787 回答