3 Repetition - Mr Lyon Computing

Download Report

Transcript 3 Repetition - Mr Lyon Computing

Python
Lesson 3 - Repetition
Python
Where do you find Python?
How do you start a new program?
Start / Computer / ICT (I:) / Python /
IDLE
In Python go to:
File / New File
How do you run a program?
What is a string?
Press the F5 key (on the top row)
A sentence. And it is usually in green
surrounded by quote marks and
brackets.
Last Lesson
You learnt about the If-Else command
favsub = “computing”
guess = input(“What is your favourite subject? ”)
if guess.lower() == favsub:
print(“That is correct!”)
else:
print(“Wrong, try again!”)
Last Lesson
You learnt the ELIF keyword to have more than two options
favsub = “computing”
guess = input(”What is your favourite subject? ”)
if guess.lower() == favsub:
print(“That is correct!”)
elif guess.lower() == “maths”:
print(“Close, Maths is my second favourite subject!”)
else:
print(“Wrong, try again!”)
Repetition
We use repetition to prevent typing the same code out many times and to make
our code more efficient.
FOR is used when you know how many times you want to do something, WHILE
is used when you don’t.
FOR Loop Example
FOR is used when you know how many times
you want to do something.
Start
x=1
x < 11
Yes
Display x
No
for x in range (1, 11):
print (x)
x=x+1
End
Save it as forloop1.
FOR Loop Example
The following program will output the
multiplication table for 7 from 1 up to 12
‘times’.
Start
x=1
x < 12
Yes
Display x*7
No
for x in range (1, 13):
print (x * 7)
x=x+1
End
Save it as forloop2.
FOR Loops
Create a program to print out the 6 times table up to 4.
for x in range (1, 5):
print (x * 6)
Save it as forloop3.
Create a program to print out the 9 times table up to 50.
for x in range (1, 51):
print (x * 9)
Save it as forloop4.
FOR Loops
Create a program to print out the 11 times table up to 28.
for x in range (1, 29):
print (x * 11)
Save it as forloop5.
Create a program to print out the 7 times table up to 75.
for x in range (1, 76):
print (x * 7)
Save it as forloop6.
FOR Loops
Create a program to print out the 21 times table up to 10.
for x in range (1, 11):
print (x * 21)
Save it as forloop7.
Create a program to print out the 50 times table up to 250.
for x in range (1, 251):
print (x * 50)
Save it as forloop8.
FOR Loops
Create a program to print out square numbers up to 5.
for x in range (1, 6):
print (x * x)
Save it as forloop9.
Create a program to print out cube numbers up to 12.
for x in range (1, 13):
print (x * x * x)
Save it as forloop10.
FOR Loops
Below is a program developed to output the lyrics of the Beatles
song ‘I Want to Hold Your Hand’. Write a program to output the lyrics
of your favourite song using a For loop for repeated lines.
Save it as forloopsong.
FOR Loop Challenge
MORE
HELP
HELP
Create a program to ask someone what times table they would like
to view. It should then output the times table they selected up to 10.
Ask the user a question using input that accepts whole numbers
Store the answer to the question in a variable called table
Use the previous activities to help you
table = int(input(“What..
for x in range (1,…)
print(???)
”))
Save it as forloopchallenge.
WHILE Loops
FOR is used when you
know how many times
you want to do
something, WHILE is used
when you don’t.
The following program
keeps on asking the user
to enter their password
until they enter it
correctly.
Start
Input
Password
password
!=
realpass
Yes
Display
“Correct”
End
No
WHILE Loop Example
The following program keeps on asking the user to enter their
password until they enter it correctly. Try it for yourself.
Save it as whileloop1.
WHILE Loops
Create a program that keeps on asking the user to enter a number
until they guess it correctly.
num = 4
guess = ””
while guess != num:
guess = int(input("Please guess a number: "))
print("You guessed the correct number!")
Save it as whileloop2.
WHILE Loops
Create a program that asks the user to guess a persons favourite
subject and keeps asking them until they get it correct.
subject = ”computing”
guess = ””
while guess.lower() != subject:
guess = input("Please guess my favourite
subject: ")
print("You guessed the correct subject!")
HINT
Do not use capitals in your subject name!
Save it as whileloop3.
WHILE Loops
Create a program that asks the user to guess a persons favourite
chocolate bar and keeps asking them until they get it correct.
subject = ”lion bar”
guess = ””
while guess.lower() != subject:
guess = input("Please guess my favourite
chocolate bar: ")
print("You guessed the correct chocolate bar –
Yummy!")
Save it as whileloop4.