Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection."— Presentation transcript:

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

2 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 Activity 1 Write the following Python program and press F5 to run. Save the program as numbers1. Place a screenshot of your code here 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.

4 Activity 2 Write the following Python program and press F5 to run. Save the program as numbers2. Place a screenshot of your code here 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.

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

6 Selection 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 End Is Age>=1 5? No Yes Input Age 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.

8 Activity 4 Write the following Python program and press F5 to run. Save the program as selection1. Place a screenshot of your code here 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.

9 Comparison Operators == 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. As well as the >= operator there are many more you can use in your programs.

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

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

12 Activity 6 Write the following Python program and press F5 to run. Save the program as selection3. Place a screenshot of your code here 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”.

13 Activity 7 Write the following Python program and press F5 to run. Save the program as selection4. Place a screenshot of your code here 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.

14 Activity 8 Write the following Python program and press F5 to run. Save the program as selection5. Place a screenshot of your code here 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!”.

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 Activity 9 Write the following Python program and press F5 to run. Save the program as selection6. Place a screenshot of your code here Create the program on the previous slide using two of your own favourite characters.

17 Activity 10 Write the following Python program and press F5 to run. Save the program as selection7. Place a screenshot of your code here 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!”

18 Activity 11 Write the following Python program and press F5 to run. Save the program as selection8. Place a screenshot of your code here 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.

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

20 Activity 13 Write the following Python program and press F5 to run. Save the program as selection10. Place a screenshot of your code here 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.

21 How Can I Improve My Coding Skills? www.codecademy.com 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 email 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 Lesson 4 - Selection."

Similar presentations


Ads by Google