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
OCR GCSE Computing Python programming 7: Program flow control OCR Computing GCSE © Hodder Education 2013 Slide 1 Python 7: Program flow control All programs can be made from 3 constructs: • Sequence – One command after another • Selection – Decision making • Iteration – repetition OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 7: Program flow control Sequence • Do this, then this, then this … #int function demo name=(input('What is your name? ')) income=int((input('What is your monthly income in pounds? '))) annual_income=income*12 print(name, ' you earn ',annual_income, ' pounds per year.') OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 7: Program flow control Selection Decision making How much money do you have? • More than £4: you can have fish and chips • More than £3: you can have fish • More than £2: you can have chips • Otherwise: nothing! OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 7: Program flow control Selection Use if… elif… else Remember the colon and the indentation: OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 7: Program flow control Selection if… elif… else What happens after an ‘if’ condition must be indented End the list of consequences by unindenting. OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 7: Program flow control Iteration This means repetition. A section of program code repeats. This is called a loop. Python has various ways of achieving this. ‘While’ and ‘for’ are the most common ways. OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 7: Program flow control Iteration with ‘while’ While a condition is true (think boolean operators) Do something The condition is tested on entry to the loop. answer='' while answer!='yes': answer=input('do you like computing? ') print('That is the correct answer') OCR Computing GCSE © Hodder Education 2013 Slide 8 Python 7: Program flow control • You can control a ‘while’ loop with a counter: • Notice that the variable ‘count’ is incremented at the end of each iteration. OCR Computing GCSE © Hodder Education 2013 Slide 9 Program flow control The ‘for’ loop This is widely used in Python. It lets you iterate a loop based on a sequence. A sequence can be lots of things: – a a string, a list, a tuple Here we use a sentence (a string). The ‘for’ loop iterates through the letters in the sentence. OCR Computing GCSE © Hodder Education 2013 Slide 10 Python 7: Program flow control The ‘for’ loop As usual with Python, notice the colon and the indentation used to indicate a program block. OCR Computing GCSE © Hodder Education 2013 Slide 11 Python 7: Program flow control The ‘for’ loop We usually want to do some processing inside a loop. Here, we count the letters ‘a’: OCR Computing GCSE © Hodder Education 2013 Slide 12 Python 7: Program flow control A for loop can be controlled with the range() function: i is a variable used here to keep track of the iterations We get an unexpected result: Python starts counting at zero and stops after 9. OCR Computing GCSE © Hodder Education 2013 Slide 13 Python 7: Program flow control We can use whatever steps we want to iterate through the loop. We give the range function the start, the finish and the step. OCR Computing GCSE © Hodder Education 2013 Slide 14 Python 7: Program flow control We can use negative steps to count backwards. OCR Computing GCSE © Hodder Education 2013 Slide 15