Python - 2024 - Y9 stage 2

Python 
Year 9
STAGE 2 Python
1 / 34
volgende
Slide 1: Tekstslide
ComputingLower Secondary (Key Stage 3)

In deze les zitten 34 slides, met interactieve quizzen, tekstslides en 1 video.

time-iconLesduur is: 50 min

Onderdelen in deze les

Python 
Year 9
STAGE 2 Python

Slide 1 - Tekstslide

PREDICT

Slide 2 - Tekstslide

What will it do?

Slide 3 - Open vraag

Slide 4 - Tekstslide

Slide 5 - Video

Investigate and Modify
Edit this code to:
  • Ask the question "Shall I repeat myself?"
  • Edit the while loop so it will keep asking the same question when they type "y" for yes
  • Edit the last print statement to "phew!  That's better" for when the looping stops

Slide 6 - Tekstslide

MAKE
Make a while loop program that:
  • Has a loop in a procedure called myloop()
  • sets a variable called "num" to the value of 1
  • uses the loop to count and print each number stored in num until it reaches 10.
  • In other words the program loops and outputs 1,2,3....,10 and then stops
  • Call this procedure in the main code section
NOTE:  You will need to make the variable "num" one bigger each time.  Think about how you would do this in maths and code it

Slide 7 - Tekstslide

Combining Selection and Iteration
Here is a typical structure you might find in code where you have a loop containing an if statement structure

As always let's step through this with PRIMM

Slide 8 - Tekstslide

PREDICT

Slide 9 - Tekstslide

What do you think this code would do?

Slide 10 - Open vraag

Investigate and Modify
Edit this code to:
  • edit the question line 8 to a general knowledge question with three options
  • edit lines 10,12 and 14 to respond appropriately with "incorrect" or "correct" feedback

Slide 11 - Tekstslide

MAKE
Use your existing program and add:
  • 2 more procedures question2() and question3()
  • Have different questions and responses for these two questions
  • Call these procedures after the question1() procedure in the main code routine

  • EXT (Hard) - Add a score variable in the main code at the first thing that happens and set this to zero.  Then add 1 to the score when they get one correct.  Print the score after the three procedures have been completed
  • TEACHER NOTE:  You will need to use a global variable for this so consider a worskshop for students who need this extension

Slide 12 - Tekstslide

Using libraries
Sometimes we need to do something in a program that is quite a common thing to do
There are libraries written with pre-existing procedures to do these things

Slide 13 - Tekstslide

Random library
To generate random numbers we can use the random module
We have to import it first and then call it when we need to use it

Slide 14 - Tekstslide

PREDICT

Slide 15 - Tekstslide

What do you think this code would do?

Slide 16 - Open vraag

Run the code

Slide 17 - Tekstslide

INVESTIGATE  and MODIFY
Change the code so that:
  • the procedure rolldice() now rolls two dice, adds them together and prints the line "You rolled a ? and a ? for a total or ?".  You should use three variables to do this and print them correctly
  • Edit the main code to ask the user for the low and high values before you call rolldiceV2()

Slide 18 - Tekstslide

Problems?

Slide 19 - Tekstslide

Datatypes
Computers must be told what kind of data they are storing.
When you input data it will always assume it is text (even when you didn't use any)
We must tell the computer it isn't text so it uses it correctly

Slide 20 - Tekstslide

RUN
Try running this code to see the error

Slide 21 - Tekstslide

INVESTIGATE and MODIFY
Try using the int() build in function to turn something into an integer (a whole number)
The brackets of the int() function go around the code that you want to be changed
Try it for yourself

Slide 22 - Tekstslide

Slide 23 - Tekstslide

MAKE
Create a program that
  • Asks you how old you are and stores this as a varible
  • checks IF this value is 18 or above
  • If it is it says you are an "adult" in a print message
  • Otherwise it tells you that you are a "child" in a message

You will need several skills to do this.  Start with this template

Slide 24 - Tekstslide

Slide 25 - Tekstslide

PREDICT

Slide 26 - Tekstslide

What do you think this code would do?

Slide 27 - Open vraag

Slide 28 - Tekstslide

INVESTIGATE and MODIFY
  • Make the program run loopit2() instead of loopit() 
  • run the program and see what it does
  • Edit loopit() to count to 50
  • Switch to running loopit() in the main code
  • run it to check it works
  • Edit loopit to start on 20 and count down to 0
  • run it to check it works

Slide 29 - Tekstslide

MAKE
Create a program that
  • sets a variable called secret to be a random number between 1 and 10
  • uses a while loop to ask the user to guess the number
  • keeps asking them until they get the right number
  • Harder - counts how many guesses it took and tells you when you get it right, how many it was.
Use the previous code to help you found here

Slide 30 - Tekstslide

PREDICT

Slide 31 - Tekstslide

Slide 32 - Tekstslide

INVESTIGATE and MODIFY
Use this edited version - You will need your adventure plans to start this task fully (twines/PPT)
  • Edit line 5 to be the name of your room
  • Edit line 28 to call changed name procedure
  • Edit flavour text intro on line 8 and change options below it to reflect player decisions
  • print suitable messages in print commands for each chosen path
  • Where you need to call a new procedure for a new location type the name as normal using brackets
  • DO NOT create these new rooms yet

Slide 33 - Tekstslide

MAKE
Whenever you need a new location  - eg hallway() , you need to define a whole new procedure for it that looks like the one you just made
It will have it's own decision, outcomes and specifics.
Essentially to create your game you are following this structure now throughout.
Try and create two more locations

Slide 34 - Tekstslide