Writing Functions( ) (Part 5)

Slides:



Advertisements
Similar presentations
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Advertisements

Control Structures FOR Statement Looping.
CPS120 Introduction to Computer Science Iteration (Looping)
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Powerpoint Templates Page 1 Powerpoint Templates Impossible Math Problems (or should we say, undecidable) Math Club 4/16/2012.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Mr Barton’s Maths Notes
Sample Surveys.
Introduction to Computing Science and Programming I
Topic: Recursion – Part 2
Adding and Subtracting Fractions with like Denominators
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
FLOWCHARTS Part 1.
Unit 3 General exam advice
Introduction To Repetition The for loop
Python: Control Structures
Algebra 7. Solving Quadratic Equations
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.
Introduction To Flowcharting
Python - Iteration Iteration
Iterations Programming Condition Controlled Loops (WHILE Loop)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
When Someone is Talking
Recursion 12-Nov-18.
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
CSE 105 theory of computation
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Exception Handling.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Repetition Structures
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Passing Parameters by value
Organization Leadership Skill Area
Loops CIS 40 – Introduction to Programming in Python
Recursion 29-Dec-18.
Writing Functions( ) (Part 4)
Conditional and iterative statements
Python programming exercise
Basics of Recursion Programming with Recursion
When Someone is Talking
For Loops (Iteration 1) Programming Guides.
The of and to in is you that it he for was.
Mr Barton’s Maths Notes
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
Flowcharts and Pseudo Code
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Recursion 23-Apr-19.
Impossible problems.
A.
Introduction to Python
CISC101 Reminders Assignment 3 due today.
CSE 105 theory of computation
Software Development Techniques
Python Simple file reading
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Iteration – While Loops
Lecture 6 - Recursion.
Presentation transcript:

Writing Functions( ) (Part 5) Loop dee loop!

Looping Functions By now, you might have realized that we can use functions to create loops in Python We can achieve this by calling a function from within the definition of the same function def loop( ): print(“Hello”) loop( )

Looping Functions BUT WAIT!!!!

Looping Functions def loop( ): print(“Hello”) loop( ) This will cause what we call an infinite loop Infinite loops are bad! And they cannot be stopped … so you may very likely lose all your work if you start one

Looping Functions So, we can control our loops by including some sort of condition … Take a moment to think about it

Practice Attempt to write a program in which you define a function that loops but will not loop continuously into an infinite one … Rather, try to control your loop In addition, try to see if you can control your loops by count! (Run a loop five times, ten times, etc.)

Looping Functions with Conditions def loop( ): print(“Hello”) choice = input(“Would you like to try again?”) if choice == “yes”: loop( )

Looping Functions by Count x = 0 def loop( ): global x if x < 5: print(“Hello”) x = x + 1 loop( )

Looping Functions with “Return” I never told you this but the keyword “return” actually has a special feature The keyword “return” actually signals a function to stop what it’s doing and go back to the main source code

Looping Functions by Return x = 0 def loop( ): global x if x = = 5: return x else: print(“Hello”) x = x + 1 loop( )

Looping Functions by Return x = 0 def loop( ): global x if x = = 5: return x else: print(“Hello”) x = x + 1 loop( ) When using the “return” keyword, if you want to actually return a value from your function, you must put the return statement under every possibility of your decision structure, even if it will chronologically never reach that line of code. Otherwise, it will return “None” (nada, nothing).

More Warnings Okay, now this is SUPER SUPER important … We will almost never use functions to loop our programs EVER. The reason being is that it’s complicated and can cause a lot of mechanical issues Let’s take a look at an example …

More Warnings This one is fine, because it has an end

More Warnings Now try this …

More Warnings The easiest way I could describe it is, that anything that follows the call of a function in the definition of a function, is put on hold when a function is called again (“or looped”) So, can we avoid it …?

More Warnings The answer here is, kind of. We will work with the looping of functions just because it is possible. However, it is IMPORTANT, OH SO IMPORTANT, to remember that if you are ever going to loop a function, do NOT put anything beyond the point at which you call the function in it’s own definition

More Warnings Best explained through an example If you follow the flow of the function, these lines are sequentially placed after the loop( ) function is called again in it’s own definition. So, we can say these lines are put on “hold” while the function loops itself and then is executed as many times as the function was called.

Tip for looping functions We’re going to use the function defined loop in our programs, but we want to be careful not to do “too much” because it will start to cause a mess and make it near impossible to follow My word of advice when it comes to these looping functions is, keep it simple. Make sure you know how the loop is controlled.

Simple Looping Function This one is fine, because it has an end

Practice Try writing a program that asks the user to guess the “magic number” If the guess is too high, then tell them “it’s too high.” If the guess is too low, then tell them “it’s too low.” But, this time let them keep trying until they finally guess the right number. Yes, you will need to define a function at some point.