2013-05-21 63 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"