Python - Iteration Iteration

Slides:



Advertisements
Similar presentations
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Advertisements

Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
Main task -write me a program
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
By the end of this session you should be able to...
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.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
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.
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.
Lesson Objectives Aims To be able to write an algorithm in Pseudo Code
Python 23 Mr. Husch.
Recap: If, elif, else If <True condition>:
Topic: Iterative Statements – Part 1 -> for loop
Week of 12/12/16 Test Review.
Formatting Output.
Introduction To Repetition The for loop
Lesson 4 - Challenges.
Selection and Python Syntax
Recursion Alice.
Lesson 05: Iterations Class Chat: Attendance: Participation
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.
Introduction to Programmng in Python
C ODEBREAKER Class discussion.
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.
Recursion Alice.
Print slides for students reference
Writing Functions( ) (Part 5)
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.
Repetition Structures
Structured Programming Taken from notes by Dr. Neil Moore
Introduction to TouchDevelop
IST256 : Applications Programming for Information Systems
Module 4 Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
random number between 1 and 5.
Problem Solving Designing Algorithms.
Playing Games.
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)
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Introduction to Repetition
Introduction to Repetition
WJEC GCSE Computer Science
Topic: Iterative Statements – Part 2 -> for loop
Directions to play game
Starter Look at the hand-out.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Week 5 - Friday CS 121.
Presentation transcript:

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 Get names. Playing = 1 GameNumber = 25 While GameNumber > 0 Get number from user GameNumber – number Show GameNumber if GameNumber > 0 if Playing = 1 Playing = 2 else If playing = 1 Player 1 loses else Player 2 loses