2013-01-15 151 views
4

我有一個關於循環的非常基本的問題。我試圖進入編程,我目前讀的書「學習Python辛苦」,並就死在研究鑽數量5前33: http://learnpythonthehardway.org/book/ex33.html我可以使用for循環而不是while循環嗎?

這是我的代碼如下所示:

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) 

但隨後,我值的每個到達循環的「頂部」時間還挺復位。

由於我對此非常陌生,請不要太過於吝嗇,如果我錯過了某些明顯的東西。

順便說一句,有人知道這本書是否有什麼好的開始或我浪費我的時間?

+0

我想不出任何辦法的參數(P)轉成'for'循環這個(除非你想在那裏放一個'break') – mgilson

+6

這是因爲'for'循環爲每次迭代設置'i'。它不會知道也不在乎你在循環中改變它。 –

+0

*「事情是我想用for循環代替while循環」 - 爲什麼? – poke

回答

4

A for Python中的循環遍歷由in之後的表達式提供的一系列值。在Python中,我們使用術語iterable來表示任何可以產生這種系列的東西。這可以是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

+0

謝謝,真的很有幫助。 – user1981659

2

for循環和while循環之間有一個關鍵區別。

for循環:執行循環體的固定次數,計算迭代次數。

while循環:只要循環的條件爲真就重複執行循環體。

通常情況下,從零開始並在特定數字(例如userloop)處停止時意味着for循環是最合適的。然而,這個程序並不是說「我將從0到數到userloop並且每次都做這些事情」;它的意思是「我從零開始,我會做這些事情,根據用戶輸入更改一個值,直到該值滿足條件(> userloop)」。

用戶可以輸入0一堆,或輸入負數,你可能永遠不會出現循環。

+0

這對於Python來說是正確的,但是對於用於C for循環的人來說,這種區別可能會令人驚訝 –

0

好吧,我想你可以嘗試在generators用於動態控制for循環

1

方向挖所以我檢查了,你鏈接到Python的堅硬方式網站上的問題......我覺得他實際上只是要求你重新寫了使用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 

你看這是怎麼回事在這?請記住,範圍函數產生如下列表:
範圍(6) - > [0,1,2,3,4,5]。
這是Python for語句知道如何迭代的東西(他們稱這些類型的東西爲iterable)。無論如何,變量i被綁定到範圍(6)中的一個項目(即列表[0,1,2,3,4,5]),每次迭代。即使你在for循環中對我做了些什麼,它也會被迭代到iterable中的下一個項目。所以增量聲明絕對沒有必要。在這種情況下,無論我是什麼,它都會增加2,但無論如何,它都會像正常情況一樣反彈。

好,畢竟這裏是我改變你的具體代碼的改變,每次改變迭代器的時候都使用while循環來使用帶有範圍的for循環。

我認爲你只是通過瀏覽鏈接到的在線書籍的前幾章來介紹函數,但是如果在還沒有看到遞歸之前可能會有點混淆。再次調用函數之前的if語句是保持當前值超出範圍內的值,這會給你一個關於超過最大遞歸深度的錯誤(python這樣做是爲了防止出現堆棧溢出錯誤我很確定)。

最後一點可以做這樣的事情與範圍:
範圍(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) 

我不認爲我會永遠這樣做,但這至少是一件有趣的事情。

1

我做到了使用此代碼,其中P是你在函數調用numba設置

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)