Presentation is loading. Please wait.

Presentation is loading. Please wait.

GCSE COMPUTER SCIENCE Practical Programming using Python

Similar presentations


Presentation on theme: "GCSE COMPUTER SCIENCE Practical Programming using Python"— Presentation transcript:

1 GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection

2 We use str to convert a number into a string.
Inputting Numbers When inputting numbers into variables you need to tell Python what data type to expect We use int to tell Python that we are going to enter an integer (whole number). We use float when working with decimal places. We use str to convert a number into a string.

3 Write the following Python program and press F5 to run.
Activity 1 Write the following Python program and press F5 to run. Create a program to ask somebody to enter two numbers. Once entered it should take the second number away from the first number and output the result. Place a screenshot of your code here Save the program as numbers1.

4 Write the following Python program and press F5 to run.
Activity 2 Write the following Python program and press F5 to run. Create a program to ask somebody to enter the height and width of a rectangle and work out the area. Think about what to call your variables carefully. Place a screenshot of your code here Save the program as numbers2.

5 Write the following Python program and press F5 to run.
Activity 3 Write the following Python program and press F5 to run. Create a program to ask somebody to enter three numbers. Once entered it should multiply them all together and output the result. Place a screenshot of your code here Save the program as numbers3.

6 Selection in flowcharts is represented using the decision symbol.
All the programs you have been developing so far have been sequential, this means that each instruction is executed in set order. With selection the path through a program can be decided by looking at a condition and then taking one of a set of alternative paths based on the result. Selection in flowcharts is represented using the decision symbol.

7 Selection Example 1 Start Input Age Is Age>=15? Yes No Display “You can watch this movie” Display “You are too young to watch this movie” To indent your text (four spaces) use the TAB key, above caps lock. End

8 Write the following Python program and press F5 to run.
Activity 4 Write the following Python program and press F5 to run. Create the program on the previous slide so when you run the program you are asked for your age. Run the program once and enter a number above 15, then again and enter a number below 15 to see the difference. Place a screenshot of your code here Save the program as selection1.

9 You can also use the logical operators AND, OR and NOT.
Comparison Operators As well as the >= operator there are many more you can use in your programs.  == Is equal to  > Is greater than  < Is less than  != Is not equal to  >= Is greater than or equal to  <= Is less than or equal to You can also use the logical operators AND, OR and NOT.

10 A program to guess your teachers favourite subject!
Selection Example 2 Start A program to guess your teachers favourite subject! favsub = “Computing” favsub = “computing” Input Subject guess = input(“What is your favourite subject?”) If guess = favsub Yes if guess.lower() == favsub: print(“That is my..”) Display “That is my fav..” No else: Display “Unlucky, try again” print(“Unlucky, try again!”) End

11 Write the following Python program and press F5 to run.
Activity 5 Write the following Python program and press F5 to run. Create the program on the previous slide, you can change the subject to whichever you please. You may also change what is printed out. Place a screenshot of your code here Save the program as selection2.

12 Write the following Python program and press F5 to run.
Activity 6 Write the following Python program and press F5 to run. Create a program to ask someone what gender they are. If they are male it should say “You are male”, if they are female it should say “You are female”. Place a screenshot of your code here Save the program as selection3.

13 Write the following Python program and press F5 to run.
Activity 7 Write the following Python program and press F5 to run. Create a program to ask someone if the are between the ages of 4 and 18, if they are it should print out “You should be at school”, if they are not print out “You are not of school age”. Hint: Use the and operator. Place a screenshot of your code here Save the program as selection4.

14 Write the following Python program and press F5 to run.
Activity 8 Write the following Python program and press F5 to run. Create a program to ask someone what their name is, if it is the same name as yours it should say “You’re cool”, else it should say ”You suck!”. Place a screenshot of your code here Save the program as selection5.

15 More Options Sometimes in a program you need more than two options. In this case we use the elif keyword. character = input(“Who is your favourite computer game character? ”) if character.lower() == “sonic”: print(“Me too! Sonic is also my favourite!”) elif character.lower() == “mario”: print(“Mario is ok but he’s overrated.”) else: print(“This character is just rubbish, why did you choose them!?”) An If statement must start with If and end with Else, however, you may use as many Elif statements in the middle as you need.

16 Write the following Python program and press F5 to run.
Activity 9 Write the following Python program and press F5 to run. Create the program on the previous slide using two of your own favourite characters. Place a screenshot of your code here Save the program as selection6.

17 Write the following Python program and press F5 to run.
Activity 10 Write the following Python program and press F5 to run. Write a program that asks the user their favourite team and if it the same as your team display “Cool team!”. If it is not the same display “They’re a rubbish team!” Place a screenshot of your code here Save the program as selection7.

18 Write the following Python program and press F5 to run.
Activity 11 Write the following Python program and press F5 to run. Update your previous program to add an Elif statement to allow a third option. For example use your second favourite team or a team from a different sport. Place a screenshot of your code here Save the program as selection8.

19 Write the following Python program and press F5 to run.
Activity 12 Write the following Python program and press F5 to run. Create a program that outputs something different for each day of the week. You will need to use multiple Elif statements. Place a screenshot of your code here Save the program as selection9.

20 Write the following Python program and press F5 to run.
Activity 12 Write the following Python program and press F5 to run. Create a program that outputs something different for each day of the week. You will need to use multiple Elif statements. Place a screenshot of your code here Save the program as selection9.

21 Write the following Python program and press F5 to run.
Activity 13 Write the following Python program and press F5 to run. Create a program that of your own choice that showcases the techniques you have learn from the first four Python lessons. It should take advantage of variables, sequencing and selection. Place a screenshot of your code here Save the program as selection10.

22 How Can I Improve My Coding Skills?
Coding is something you need to practice often to get good at it. Codecademy is a website that allows you to practice the syntax of Python (..and other languages). Best of all, its free and fun! Sign up using a personal address and start working through the Python course. ACTIVITY Start working through each exercise to practice.


Download ppt "GCSE COMPUTER SCIENCE Practical Programming using Python"

Similar presentations


Ads by Google