lundi 10 avril 2017

Does importing random affect use ow while loops?

This code snippet is from a python course being taught at a middle school. The file imports turtle and then random to allow for colors to be selected from a list while the square is being drawn. I tried changing the if to a while loop, and got an unexpected result. The loop continues after num = 0 indefinately. I tried changing line 8 by removing the decrement outside of the call to sqspin(num) and doing the decrement on the line above like so: num = num -1 This change started creating random changes to the value of num for each successive loop. I don't understand why while > 0 doesn't terminate the loop, or where those random values for num are coming from. Any clues? I'm running 3.4.3, but the school is using 2.7.

  def sqspin(num):
      print(num)
      if num > 0: 
         sq()
         rt(5)
         mycolor = pickcolor()
         pencolor(mycolor)
         sqspin(num-1)

sqspin(5)




Aucun commentaire:

Enregistrer un commentaire