Conditional loops are way to repeat something while a certain conditionis satisfied, orTrue. If the condition is always satisfied (neverbecomesFalse), the loop can become infinite.If the condition starts off false, the code in the loop will never run!In Python conditional loops are defined with thewhile statement:
word=''sentence=''print('Please enter some words.')print('Include a period (.) when you are finished.')while'.'notinword:word=input('next word: ')sentence=word+' '+sentenceprint()print('Aha! You said:')print(sentence)
We call this part of the code the ‘conditional’:'.'notinword
Whether the conditional return True or not determines whether the code insidethewhile loop runs.
Read the code above, and see if you can summarise in your head whatit what it do (what its final output will be).
Then copy it into a file, saysentence.py and run it - seeexactly what it does. Does that match up with what you thought?
Note
If you are using Python 2, you will need to replaceinput withraw_input to run the program correctly.
The turtle has been up to its usual tricks again, robbing liquorstores and building up huge gambling debts. It’s time for turtle to beput into a cell that it can’t get out of.
Let’s make a new version offorward(). One that will turn the turtlearound if it tries to go further than 100 from the origin. We’ll needawhile loop, and some new turtle functions:
turtle.distance(0,0) - Returns the distance of the turtle fromthe origin (0, 0)turtle.towards(0,0) - Returns the angle to get back to origin (0, 0)turtle.setheading(angle) - Directly sets the turtle’s directionYou could try playing with a turtle in the interpreter and using thesefunctions to check exactly what they do, if you like.
Now you will need to implement the prison logic using these turtlefunctions, perhaps awhile loop and a bit of conditional logic.It’s a bit of a stretch but keep at it! Don’t be afraid to talk it outwith a coach or another student.
defforward(distance):whiledistance>0:ifturtle.distance(0,0)>100:angle=turtle.towards(0,0)turtle.setheading(angle)turtle.forward(1)distance=distance-1
Loops can be interrupted with thebreak statement. This isespecially useful if you write aninfinite loop, which is a loopwhere the conditional is alwaysTrue.
Write awhile loop with a condition that is alwaysTrueto draw a spiral. Interrupt the loop when the turtle reaches a certain distancefrom the center. Use the functionturtle.distance(x,y) to get theturtle’s distance to the point defined by the coordinatesx andy.
To do this you will need theturtle.xcor() andturtle.ycor()functions, which return the position of the turtle in X and Y axesrespectively.
Note
To draw a spiral, the turtle has to rotate by a constant value and moveforward by an increasing value.
defdraw_spiral(radius):original_xcor=turtle.xcor()original_ycor=turtle.ycor()speed=1whileTrue:turtle.forward(speed)turtle.left(10)speed+=0.1ifturtle.distance(original_xcor,original_ycor)>radius:break
Can you make a conditional for this loop, so you don’t need theinfinite loopwhileTrue or thebreak? Which version do you findeasier to understand?