Unformatted text preview:

COMP 116 WHILE LOOPS Instructor Jason Carter WHILE LOOPS THE WHILE LOOP THE WHILE LOOP THE WHILE LOOP In order for a loop to stop executing something has to happen inside the loop to make the condition false Iteration one execution of the body of a loop while loop is known as a pretest loop Because it tests a condition before go into a loop there s a chance that you don t go into a loop Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the loop INFINITE LOOPS Loops must contain within themselves a way to terminate Something inside a while loop must eventually make the condition false Infinite loop loop that does not have a way of stopping Repeats until program is interrupted Occurs when programmer forgets to include stopping code in the loop CALLING FUNCTIONS IN A LOOP Functions can be called from statements in the body of a loop Often improves the design Example Write a function to calculate then display the commission for a sales amount Call the function inside a loop LOOP PRACTICE LARGEST INPUT Write the function largestInput this takes no parameters repeatedly reads integers from the user stopping when the user enters 1 and print the largest value input WHILE LOOP GENERAL ALGORITHM while some condition is true Do this code Something here should modify the condition above LOOPS THAT COUNT These loops are equivalent PRACTICE Write a loop that counts from1 to 100 and only prints odd numbers STANDARD LIBRARY FUNCTIONS AND THE IMPORT STATEMENT Just like we have built in functions like print and input we also have libraries of other pre written functions Modules files that stores functions of the standard library Help organize library functions not built into the interpreter Copied to computer when you install Python To call a function stored in a module need to write an import statement Written at the top of the program Format import module name GENERATING RANDOM NUMBERS Random number are useful in a lot of programming tasks random module includes library functions for working with random numbers Dot notation notation for calling a function belonging to a module Format module name function name GENERATING RANDOM NUMBERS randint function generates a random number in the range provided by the arguments Arguments Function call GENERATING RANDOM NUMBERS randint function generates a random number in the range provided by the arguments Returns a number between 1 and 100 The number variable is set equal to the return value of random randint 1 100 PRINTING A RANDOM NUMBER RANDOM NUMBERS EXAMPLE RANDOM NUMBERS PRACTICE Guess the Number Write a program that Randomly generates an integer between 0 and 100 Continue to prompt your user to enter numbers until they enter the randomly generated number Think while loop To make the game a little easier tell the user if their guess was too low or too high and of course be sure to tell them when they guess correctly Sample Output


View Full Document

UNC-Chapel Hill COMP 116 - While Loops

Download While Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view While Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view While Loops and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?