Starter Look at the hand-out.

Slides:



Advertisements
Similar presentations
How SAS implements structured programming constructs
Advertisements

Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
By the end of this session you should be able to...
CPS120 Introduction to Computer Programming The Programming Process.
Python 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.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Topic: Iterative Statements – Part 1 -> for loop
Week of 12/12/16 Test Review.
Lesson 4 - Challenges.
Selection and Python Syntax
Topics Introduction to Repetition Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
PROGRAM CONTROL STRUCTURE
IF statements.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS1371 Introduction to Computing for Engineers
Lesson 3 - Repetition.
Python - Iteration Iteration
Topics Introduction to Repetition Structures
Iterations Programming Condition Controlled Loops (WHILE Loop)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Print slides for students reference
Adapted from slides by Marty Stepp and Stuart Reges
Intro to Computer Science CS1510 Dr. Sarah Diesburg
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Python I/O.
exa.im/stempy16.files - Session 12 Python Camp
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Exception Handling.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Repetition Structures
Topic 1: Problem Solving
Loops CIS 40 – Introduction to Programming in Python
Computational Thinking for KS3
Coding Concepts (Basics)
Introduction to TouchDevelop
IST256 : Applications Programming for Information Systems
Module 4 Loops.
Adapted from slides by Marty Stepp and Stuart Reges
Topics Introduction to Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Problem Solving Designing Algorithms.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Python programming exercise
Using the Target Variable Inside the Loop
PYTHON: BUILDING BLOCKS Sequencing & Selection
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Nested If Statements While Loops
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Topics Introduction to Repetition Structures
Introduction to Repetition
Introduction to Repetition
WJEC GCSE Computer Science
Directions to play game
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Starter Look at the hand-out. Could you program the robot to light all the squares up? What instructions would you give it?

Python - Iteration Iteration Iteration is a posh way of saying “loop” (iteration literally means to do something again). Loops are absolutely vital in programming in order to avoid having to write sequences of code out again and again. And they come in several forms:

Python - Iteration While Loops A WHILE loop is a way of repeating a section of code. It is easy to understand because it uses something that looks a lot like an IF statement at the top.

Python - Iteration How many times do you think this loop will repeat? Try it and find out: number = 1 while number < 10: print(“This is turn ”,number) number = number + 1 print(“The loop is now finished”) Make sure you understand why the last line only prints out once, at the end of the loop.

Python - Iteration A WHILE loop is ideal if you don’t know exactly how many times you will need to repeat the code.

Python - Iteration targetNumber = 7 # ask for a guess # this is the correct number targetNumber = 7 # ask for a guess guess = int(input(“Guess a number between 1 and 10”)) # repeat if the answer is wrong while guess != targetNumber: print(“Wrong, try again”) guess = int(input(“Guess a number between 1 and 10”)) # do this after the loop print(“Congratulations - that’s right!”)

Python - Iteration Tasks Create a program that gets two players to enter in their names and then you choose a Gamenumber between 10 and 50. Each player takes it in turns to choose either 1,2 or 3. This number is then deducted from the Gamenumber. The last person to deduct a number from the Gamenumber before it reaches 0 loses.

Python - Iteration Solution – Pseudo Code Playing = 1 GameNumber = INPUT Valid = false While GameNumber > 0 While Valid = false Get number from user if number 1,2 or 3 and Gamenumber – number >= 0 Valid = true GameNumber – number Show GameNumber if GameNumber > 0 if Playing = 1 Playing = 2 else IF playing = 1 THEN OUTPUT “Player 2 loses” ELSE OUTPUT “Player 1 loses” END IF