Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Functions( ) (Part 5)

Similar presentations


Presentation on theme: "Writing Functions( ) (Part 5)"— Presentation transcript:

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

2 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( )

3 Looping Functions BUT WAIT!!!!

4 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

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

6 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.)

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

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

9 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

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

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

12 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 …

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

14 More Warnings Now try this …

15 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 …?

16 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

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

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

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

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


Download ppt "Writing Functions( ) (Part 5)"

Similar presentations


Ads by Google