Download presentation
Presentation is loading. Please wait.
1
Starter What does the following code do?
Extension: What sort of statement has been used here?
2
Python: Selection Statements
3
Objectives – 2.1.7 Candidates should be able to:
(a) understand algorithms (written in pseudocode or flow diagram), explain what they do, and correct or complete them (b) produce algorithms in pseudocode or flow diagrams to solve problems. (h) understand and use selection in an algorithm (IF and CASE statements)
4
Algorithms Understanding how to solve the problem is important. You cannot just start coding at line 1 and hope to get a working solution straight away Algorithm is defined as: a series of steps to solve a problem (think cooking recipes) Algorithms may be written in pseudocode Pseudocode is when we write an algorithm in programming-style constructs but not in an actual programming language.
5
Example Input diameter If diameter <0 “Please enter a diameter greater than 0” Else circumference = 3.141*diameter Output “Circumference is ” + circumference
6
Activity Write an algorithm (on paper) that will solve the problem of calculating a dog’s age. You should work in pairs. The formula is: 10.5 dog years per human year for the first 2 years, then 4 dog years per human year for each year after.
7
Solution Print “enter dog’s age in human years” If age <= 2 dogage = age * 10.5 Else dogage = (2*10.5) + ((age-2)*4) Output age, “in dog years is ”, dogage
8
Python – Selection hints
A=5 - Selection if a==b: - Comparison Indentation! The code that you want python to run if your test is met must be indented. if a==b: print(‘snap’)
9
Activity 2 Program your algorithm using Python
If you need guidance look in the resources section on BHL Hint: Remember the different data types!
10
You are going to some form of conditional statement to create a menu for a basic text-based calculator program. The menu should be displayed as below: Please enter the number of the operation you wish to perform: Addition Subtraction Multiplication Division Exit The program should then prompt the user for two numbers before performing, and outputting, the selected operation.
11
Homework You need to complete you calculators
Some of you may wish to make more scientific versions
12
Plenary What is the purpose of an algorithm?
Identify the following in this section of code: Variable Selection statement Operator Casting Step-by-step instructions to solve a problem
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.