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)
Loops

2 Random Numbers Python has the ability to generate random numbers
We can achieve this using the randint( ) function The randint( ) function takes two arguments as the inclusive boundaries from which to choose a value from, it then returns an integer value at random

3 Random Numbers However, randint( ) is not a function that is embedded into Python’s main shell Therefore, we need to import a file called “random”, then we can call the specific function from the file

4 Random Numbers

5 Practice Define a function that recreates the guessing magic number game but this time, make the magic number randomly generated Make sure the function includes statements for whether the guess was too high or too low

6 Randomizing Statements
It would be kind of weak if we could only generate random integers and that was it We can extend these random integers into randomizing statements and other values

7 Practice Write a program of my favorite game, the one I used to lure all of you into this class … Recreate the game rock, paper, scissors on Python and have your computer play fair by random!

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

9 Looping Functions BUT WAIT!!!!

10 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

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

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

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

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

15 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

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

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

18 Practice #Input: passes one integer argument
#Processing: checks for lowest integer factor #Output: returns integer value of lowest prime factor Loop a function so that you can check a number for the lowest possible factor of that number and then return it into a variable. Then print it out.

19 Practice See if you can recreate the rock paper scissors game to continuously run until the user does not want to play anymore OR until the user wins 5 times OR play best out of 3


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

Similar presentations


Ads by Google