Writing Functions Part 5 Loop dee loop Looping

  • Slides: 20
Download presentation
Writing Functions( ) (Part 5) Loop dee loop!

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

Looping Functions • By now, you might have realized that we can use functions

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 BUT WAIT!!!!

Looping Functions def loop( ): print(“Hello”) loop( ) • This will cause what we

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

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

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

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:

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”

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 = =

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 = =

Looping Functions by Return x=0 def loop( ): global x if x = = 5: return x else: print(“Hello”) x=x+1 loop( ) return x 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 important … • We will almost

More Warnings • Okay, now this is 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 • This one is fine, because it has an end

More Warnings • Now try this …

More Warnings • Now try this …

More Warnings • The easiest way I could describe it is, that anything that

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

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

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

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

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

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.