2013-05-21 71 views
0

我是python的初學者,但是我已經使用了其他語言,並且我編寫了這個應該是連續循環的簡單程序。連續循環不工作 - Python

該程序應顯示並計數到360,然後從0開始。我會感謝任何反饋或建議。這是我的代碼。

import time 

currentRotation = 0.00 
turn = "Yes" 
start = 1 

while start == 1: 
    while turn == "Yes": 
     while currentRotation < 360: 
      time.sleep(0.005) 
      currentRotation = currentRotation + 0.01 
      print("Current Rotation =", currentRotation) 

     else: 
      currentRotation = 0.00 
      turn = "No" 

    else: 
     turn = "yes" 
+4

你必須改變反過來=「是」轉=「是」,使其工作:) – Mikhus

+0

@Mikhus - 我剛來到這個結論之前,我看到您的評論。這就是答案。發表它。 – mgilson

+2

+1 for @Mikhus,另外,你可以通過使用1和0來代替字符串來避免這些問題並保存了一個你永遠不會真正使用的var聲明 – Andenthal

回答

1

正如Mikhus說,讓你的無限循環,你確實有更改最後一行:

turn = "yes" 

到:

turn = "Yes" 

,因爲如果你比較依次單擊「Yes 「在你的循環中,如果你將它設置爲」是「,它會返回false,它們不一樣。

這是更好的整體:

import time 

currentRotation = 0.00 

while True: 
    while currentRotation < 360: 
     time.sleep(0.005) 
     currentRotation += 0.01 
     print("Current Rotation =", currentRotation) 

    else: 
     currentRotation = 0.00 
0

我發現在你的代碼兩件事情:錯字「是」,而不是「是」,而且這兩個換行符的其他句子做出之前的代碼無法正常工作。此代碼爲我工作:

import time 

currentRotation = 0.00 
turn = "Yes" 
start = 1 

while start == 1: 
    while turn == "Yes": 
     while currentRotation < 360: 
      time.sleep(0.005) 
      currentRotation = currentRotation + 0.01 
      print("Current Rotation =", currentRotation) 
     else: 
      currentRotation = 0.00 
      turn = "No" 
    else: 
     turn = "Yes"