Learning Intentions
TBAT understand about functions in Python and how they work Success Criteria You will be able to write your own functions and create a number guessing game |
Keywords
|
Make a function
This is a starter activity and should take 5 minutes
1. Log in to your computer
2. Open IDLE
3. In interactive mode create this function:
This is a starter activity and should take 5 minutes
1. Log in to your computer
2. Open IDLE
3. In interactive mode create this function:
4. Press return untill the >>> prompt returns, then type this line
>>> count(10)
>>> count(10)
Times Tables
You can use Python to produce any times tables, it's really easy with a "While" loop.
So how do we do this?
You will need to supply a value that indicates which times table we want, as this is a number we could call this argument num:
This is what it would look like in your code
def times_tables(num):
Next you will need to produce a "while" loop but we need to work out the first line of code for one line of the table like this 2 x 5 = 10.
The 2 is counter that will be used in the while loop, we will use "n" for this variable.
The 5 is "num" and the 10 is achieved by multiplying "n" by "num"
All of this can be put together in just one print statement.
You can use Python to produce any times tables, it's really easy with a "While" loop.
So how do we do this?
You will need to supply a value that indicates which times table we want, as this is a number we could call this argument num:
This is what it would look like in your code
def times_tables(num):
Next you will need to produce a "while" loop but we need to work out the first line of code for one line of the table like this 2 x 5 = 10.
The 2 is counter that will be used in the while loop, we will use "n" for this variable.
The 5 is "num" and the 10 is achieved by multiplying "n" by "num"
All of this can be put together in just one print statement.
What if we want to produce a times table that is twelve lines long so that you get 12 x 5 =60
TASK
Edit your code to produce the 5 times table up to 12 x 5 = 60
Adding multiple arguments into a function.
Going back to the code and changing it every time you want to recalcuate a times table can be a bit of a pain. We can add arguments to the function to overcome this.
TASK
Edit your code to produce the 5 times table up to 12 x 5 = 60
Adding multiple arguments into a function.
Going back to the code and changing it every time you want to recalcuate a times table can be a bit of a pain. We can add arguments to the function to overcome this.
Type in the code above, have you noticed the extra argument?
Press return twice then type in this.....
>>> times_tables(12, 17)
The number guessing game
Open up a new window
Press return twice then type in this.....
>>> times_tables(12, 17)
The number guessing game
Open up a new window
Group 1 type in this code and as you type try to work out what each line is doing.
save the file as myNumber.py Idea 1 - Make the "mynumber.py" game easier for younger children by reducing the range of numbers between 1 and 100 to between 1 and 10
Idea 2 - Get the "myNumber.py" game to offer a range of levels Easy : choose between numbers 1 to 10 Medium: numbers up to 20 Hard: numbers between 1 and 100 click here for a HINT Group 2 copy the code into a new window in IDLE and save it as "myNumber.py". Run the code and play the game. Try to work out what each line is doing.
Idea 1 - copy the "times_table" code into a new window and save it as "times_table.py" and add some code so that it stops the program nicely. Idea 2 - Next try to add some code to make it count and disply how many guesses it took the player (HINT - you will need to create anothe variable which you should call "counter".) Remember to save your code. |