yr 9 a computer science nice to meet you variable challenge! 1.variable assessment looping in python...

28
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

Upload: alan-davidson

Post on 23-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Yr 9 a computer science

Nice to meet you variable challenge!1.Variable assessment

Looping in python2.Looping assessment

Drawing in python using turtle

Page 2: 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 hexadecimalToday we are going to spend majority of time coding completing assessments

Page 3: 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

Objectives

To understand how to use variables To create a program with a variableComplete a loop using python: a while loop,a for loop and a

Page 4: 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

Program 1-nice to meet youprint("Please type your name in")my_name = input ()print("Nice to meet you "+ my_name)

Make sure that you save your program as My_name.py

Run your program once saved by pressing f5

**if using python 3.2 then do not use raw_input use input instead**

Page 5: 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

Make sure

When you run your program that you put your name in speech marks!

Otherwise it wont work!!

Page 6: 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

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

Page 7: 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

Variable assessment

for 3 lines of extra code you get 3 pointsFor 2 lines of extra code you get 2 pointsFor 1 line of extra code you get 1 point

See who can be the coding champion

Page 8: 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

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.

Page 9: 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

Looping in python

Page 10: 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

Loops strings and tuples-loopy 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.pyThen press f5 and run

Page 11: 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

Loopy Program explainedThe new idea in this program is the for loop Ie:For letter in word:

Print letterExplain the code:All sequences are made up of elementsA string is a sequence in which each element is one characterIn 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 sequenceIn 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 elementIn the loop body ,I simply print LETTER

Page 12: 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

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 keeplooping

Page 13: 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

Task 1

Write a while loop to ask someone what the best football team in the world is. Repeat thequestion until they say the correct team.

Page 14: 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

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 = 6guesses = 3while 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!")breakelse:guesses = guesses 1if guesses == 0:

print("You have no guesses left!")

In this code, we also learn of the breakstatement.

This will tell the PC to skip to the next line of code that is not in the current indented nesting.

Page 15: 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

Loops harder stillThe code below shows an example of how to add a list of numbers together:

count = 5number = 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)

Page 16: 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

Loop assessment 15 – For Loops

Write a for loop that will print the following sentence 20 times:

“I must listen to my IT teachers at all times, because they are very intelligent”

Page 17: 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

Using turtle module in python

Page 18: 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

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.

Page 19: 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

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.

Page 20: 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

If you have used Scratch, the turtle module may seem familiar to you.They are both descended from the LOGO programming language.

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).

Page 21: 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

You can use the “goto” method to move the turtle to a position on the screen by its co-ordinates.

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.

Page 22: 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

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 ....

Page 23: 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

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.

Page 24: 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

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.

Page 25: 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

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) / 360step = 0.01745 ...

For my version, I decided that 0.02 was close enough.

Page 26: 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

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.

Page 27: 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

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?

Page 28: 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