Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 3 - Repetition.

Similar presentations


Presentation on theme: "Lesson 3 - Repetition."— Presentation transcript:

1 Lesson 3 - Repetition

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

3 You learnt about the If-Else command
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!”)

4 You learnt the ELIF keyword to have more than two options
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!”)

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

6 FOR is used when you know how many times you want to do something.
FOR Loop Example FOR is used when you know how many times you want to do something. Start x = 1 x < 11 No for x in range (1, 11): print (x) Yes Display x x = x + 1 End Save it as forloop1.

7 FOR Loop Example for x in range (1, 13): print (x * 7)
The following program will output the multiplication table for 7 from 1 up to 12 ‘times’. Start x = 1 x < 12 No for x in range (1, 13): print (x * 7) Yes Display x*7 x = x + 1 End Save it as forloop2.

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

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

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

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

12 FOR Loops Save it as forloopsong.
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.

13 FOR Loop Challenge 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 HELP table = int(input(“What ”)) for x in range (1,…) print(???) MORE HELP Save it as forloopchallenge.

14 WHILE Loops FOR is used when you know how many times you want to do something, WHILE is used when you don’t. Start Input Password password != realpass The following program keeps on asking the user to enter their password until they enter it correctly. No Yes Display “Correct” End

15 WHILE Loop Example Save it as whileloop1.
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.

16 WHILE Loops Save it as whileloop2. num = 4 guess = ””
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.

17 Do not use capitals in your subject name!
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.

18 WHILE Loops Save it as whileloop4. subject = ”lion bar” guess = ””
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.


Download ppt "Lesson 3 - Repetition."

Similar presentations


Ads by Google