Presentation is loading. Please wait.

Presentation is loading. Please wait.

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.

Similar presentations


Presentation on theme: "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control."— Presentation transcript:

1 OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control

2 OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 7: Program flow control All programs can be made from 3 constructs: Sequence – One command after another Selection – Decision making Iteration – repetition

3 OCR Computing GCSE © Hodder Education 2013 Slide 3 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.')

4 OCR Computing GCSE © Hodder Education 2013 Slide 4 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!

5 OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 7: Program flow control Selection Use if… elif… else Remember the colon and the indentation:

6 OCR Computing GCSE © Hodder Education 2013 Slide 6 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.

7 OCR Computing GCSE © Hodder Education 2013 Slide 7 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.

8 OCR Computing GCSE © Hodder Education 2013 Slide 8 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')

9 OCR Computing GCSE © Hodder Education 2013 Slide 9 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.

10 OCR Computing GCSE © Hodder Education 2013 Slide 10 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.

11 OCR Computing GCSE © Hodder Education 2013 Slide 11 Python 7: Program flow control The ‘for’ loop As usual with Python, notice the colon and the indentation used to indicate a program block.

12 OCR Computing GCSE © Hodder Education 2013 Slide 12 Python 7: Program flow control The ‘for’ loop We usually want to do some processing inside a loop. Here, we count the letters ‘a’:

13 OCR Computing GCSE © Hodder Education 2013 Slide 13 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.

14 OCR Computing GCSE © Hodder Education 2013 Slide 14 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.

15 OCR Computing GCSE © Hodder Education 2013 Slide 15 Python 7: Program flow control We can use negative steps to count backwards.


Download ppt "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control."

Similar presentations


Ads by Google