Yr 9 a computer science

Download Report

Transcript Yr 9 a computer science

Yr 9 a computer science
Nice to meet you variable challenge!
1.Variable assessment
Looping in python
2.Looping assessment
Drawing in python using turtle
Last session
we covered binary and hexadecimal
Today we are going to spend majority of time
coding completing assessments
Objectives
To understand how to use variables
To create a program with a variable
Complete a loop using python: a while loop,a for loop
and a
Program 1-nice to meet you
print("Please type your name in")
my_name = input ()
print("Nice to meet you "+ my_name)
**if using python 3.2 then do not use raw_input use input instead**
Make sure that you save your program as
My_name.py
Run your program once saved by pressing f5
Make sure
When you run your program that you put your
name in speech marks!
Otherwise it wont work!!
Program 2-extension
print("Please type your name in")
my_name = input ()
print("Nice to meet you " + my_name)
print("So, " + my_name + ", what is your favourite food?")
favourite_food = input ()
print("Ah, your favourite food is " + favourite_food)
Once typed in save , the run by pressing f5
You will need to save this as questions .py
Variable assessment
for 3 lines of extra code you get 3 points
For 2 lines of extra code you get 2 points
For 1 line of extra code you get 1 point
See who can be the coding champion
Extension task
Consider adding more complex questions into the script.
Notice in the 4th line above it is necessary to have two + signs, one
either side of the variable my_name.
Nb :Common errors to watch for are not having a complete pair of
double quotation marks or adding + signs between variables and
strings.
Looping in python
Loops strings and tuplesloopy string program
This program takes a word from the user and prints its
letters in order on separate lines
This program provides a good example of a for loop
Type this code in then save as
loopy_string.py
Then press f5 and run
Loopy Program explained
The new idea in this program is the for loop
Ie:
For letter in word:
Print letter
Explain the code:All sequences are made up of elements
A string is a sequence in which each element is one character
In the case of the string loop , the first element is the character “l” ….
A for loop marches over or iterates over a sequence one element at a time
A for loop uses a variable that gets each successive element of the sequence
In my loop the ,letter is the variable that gets each successive character of
“loop”.
Inside the loop body the loop can then do something with each successive
element
In the loop body ,I simply print LETTER
Looping-while loops
Computers can be programmed to continue repeating code while a condition is
True.
Look at the example below for an idea
name = ""
while name != "Batman":
print("Somebody call Batman!")
print("Are you him? ")
name = input("What's your name?")
As you can see in the example in the left, the code will keeping looping and
asking the same question while the answer is not Batman. Notice the nesting of
statements underneath the while? It is the indented code that will keep
looping
Task 1
Write a while loop to ask someone what the
best football team in the world is. Repeat the
question until they say the correct team.
Harder loops!
Now we will try to write one more while loop this time using a condition.
Again this task is a lot harder. Look at the example below and try to write
your own.
secret_number = 6
guesses = 3
while guesses > 0:
print("Can you guess what number I am thinking?)
guess = int(input("write a number "))
if guess == secret_number:
print("Well done, you guessed correctly!")
break
else:
In this code, we also learn of the break
statement.
guesses = guesses 1
if guesses == 0:
print("You have no guesses left!") This will tell the PC to skip to the next
line of code that is not in the current
indented nesting.
Loops harder still
The code below shows an example of how to add a list of
numbers together:
count = 5
number = 0
print("this programme will work out the total of a
list of numbers")
while count > 0:
number = number + int(input(print("Please enter a
number :")))
count = count 1
print(number)
Loop assessment 1
5 – For Write a for loop that will print the following
Loops sentence 20 times:
“I must listen to my IT teachers at all times,
because they are very intelligent”
Using turtle module in python
Agent P, I have been hearing some
rumours about a Python Turtle.
Please find it and invite it to join O.W.A.C.A.!
Aims:
For students to gain some experience with Python's turtle
module.
Objectives:
ALL:
Use the turtle module in the interpreter.
MOST: Experiment with simple drawings using turtle.
SOME: Make simple programs using the turtle module and
explore some of its more advanced features.
Have a go at using these basic commands.
First we create an instance of the Turtle class.
I called it 'a', you can call it what you like.
Then we can use the dot notation to invoke the turtle's methods.
a.fd(n)
-> go forward n pixels.
a.lt(n)
-> turn left n degrees.
a.color(n)
-> change the color of the line.
a.home()
-> go back to 0,0.
a.pensize(n) -> change the width of the line.
a.up()
-> take the pen off the paper.
Have a play with the turtle object.
Can you work out how to ... ?
-> turn right
-> go back
-> put the pen down again
Mini Tasks
In the interpreter:
-> draw a triangle
-> draw a pentagon
-> draw a circle (don't use the built-in method).
If you have used Scratch, the turtle module may seem familiar to you.
They are both descended from the LOGO programming language.
You can use the “goto” method to move the
turtle to a position on the screen by its coordinates.
The center of the turtle's window is 0,0.
You can write words in the turtle window.
You can also change the turtle to a turtle shape
and stamp an imprint on the screen.
If you type the name of your turtle object and a
dot, IDLE will show you a list of methods that
you could invoke.
Have a play and find out what some of them
do.
If you succeeded with the triangle and
pentagon scripts, you probably noticed that
you could generalise the procedure.
And a circle is just a polygon with very many
sides ....
I asked the program to draw a polygon with
360 sides, all of length 1.
Task:
For the next activity, we need a function that will draw a
quarter of a circle.
See if you can work this out for yourself.
If you can, I want something that will draw this shape.
It is just two quarter-circles facing one another.
Turtle Challenge
Once you have the “petal” shape function, you
should be able to write a program that draws
flowers like this.
Choose some nice colours and vary the
number of “petals” in the flower until you find a
design that you like.
Only if you are stuck ...
Here's a possible quarter-circle function.
circumference = π * 2 * radius
We are going to draw the arc in steps corresponding to one degree.
Let's call the length of these “step”.
step = (π * 2 * radius) / 360
step = 0.01745 ...
For my version, I decided that 0.02 was close enough.
Using fill
Just use begin_fill() before you start drawing the shape you want to fill.
Then, use end_fill() and the shape will be filled for you.
Task:
Have a play with that filled square routine.
If you alter the amount of turn you can write a function that produces results
similar to these.
You'll probably want to use a while loop. How will your program know when it is
time to stop?