Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Python while loops Girls’ Programming Network School of Information Technologies University of Sydney Mini-lecture 6 Loops while loops Summary 2 Outline 1 Loops 2 while loops 3 Summary Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 3 Our programs usually don’t run for very long • Because we can only execute a few lines and then the program finishes • We could make it longer by duplicating the code • But it would always be a fixed number of lines • So we need some way of doing things over and over again • The idea of repetition is called looping or iteration Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 4 Different languages, different loops • All procedural languages provide control structures for: • making decisions (if statements) • repeating things over and over (while, for, do-while, repeat-until loops) • Almost all procedural languages have if statements • But there’s lots of variation across languages about loops • Python has two types of loops: while loops and for loops Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 5 A Python while loop in action 1 2 3 4 5 6 7 8 9 >>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>> • A while loop starts with the keyword while Remember, all Python control structures start with a keyword • Next is the conditional expression i < 3 and then a colon Watch out for missing the colon! Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 5 A Python while loop in action 1 2 3 4 5 6 7 8 9 >>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>> • A conditional expression is either evaluates to True or False • Here the comparison operator < tests if i is less than 3 Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 6 A while loop simply a repeating if statement 1 2 3 4 5 6 7 8 9 >>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... i += 1 ... 0 is less than 3 1 is less than 3 2 is less than 3 >>> • If the condition is True, run the loop body again • The body is indented just like the body in an if statement • The i += 1 statement is the same as i = i + 1 • It adds one to the value in i each time it is run Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 7 The body must change the condition! • If not, we’ll never ever stop looping 1 2 3 4 5 6 7 >>> i = 0 >>> while i < 3: ... print i, 'is less than 3' ... 0 is less than 3 0 is less than 3 ... • We’ve removed the i += 1, so the condition doesn’t change • This is called an infinite loop • You can interrupt an infinite loop by pressing Ctrl-C Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 8 General pattern for making while loops • Your while loops will (usually) do the following: 1 Setup the loop variable(s) (the initialisation) 2 Test whether to continue running loop (the condition) 3 Run the body if the condition is true (the body) 4 Update the loop variable (the update) • You should check each time to make sure they’re all there! Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 9 We’ve seen looping over a list of numbers 1 2 3 4 5 6 7 8 9 >>> i = 1 >>> while i < 12: ... print i, 'times 5 =', i*5 ... i += 1 ... 1 times 5 = 5 2 times 5 = 10 ... >>> • i is the loop variable and is initialised to 1 • i < 12 is the condition • print i, 'times = 5', i*5 is the body • i += 1 is the update step Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 10 We can also loop over user input • use raw_input inside the loop as the update: 1 2 3 4 5 6 >>> number = int(raw_input('Enter a number? ')) >>> while number != 73: ... print 'Wrong! try again.' ... number = int(raw_input('Enter a number? ')) ... >>> • The raw_input lines are both initialiser and update Girls’ Programming Network while loops Mini-lecture 6 Loops while loops Summary 11 You should now be able to: • Explain why programming languages need loops • Write Python while loops • Interrupt an infinite loop in Python • Read user input using a loop Girls’ Programming Network while loops Mini-lecture 6