![]() However, they always stopped when we reached the value 10. Do the numbers change when you run the program multiple times?Īs we see with this example, the same loop ran a different number of times each time we ran the program.When you run the program again, how many times did the loop execute?.When you run the program the first time, how many times did the loop execute?. ![]() Type the following code and run the file: from random import randint When you call this function, it will generate a random integer that falls between and/or including a and b.Ĭreate a file in your lesson directory called thats_random.py. The function that we will use is randint(a,b). In this function, a is the starting integer and b is the ending integer (very similar to the range function we learned in the FOR loop lesson. In this case, the random library has multiple different functions that generate all sorts of random numbers. To generate random numbers, we use the Random Library. If you remember libraries from our first lesson, they are just collections of tools that add on to the Python programming language. P.S.: While loops can go on forever if you set the condition in the while loop to be “1” or “True”.An example of running a while loop until it finishes without knowing the end point can be seen using random numbers. Have the condition be true only until a variable passes 10. Program your CoDrone Mini to do pushups using a while loop. For each question, do you get True, False, or an Error?: 382 > 438 Now that we have a better understanding of how these types of comparisons work, let’s try some exercises on your own. Less Than or Equal To (2 #Should get True.Less Than (=) : Is the value on the left bigger or equal to the value on the right?.Greater Than (>) : Is the value on the left bigger than the value on the right?.There are a number of different logical operators: Python uses many of the logical operators that you will see in math class and they function in the same way. When creating conditions, we will often want to make different comparisons based on mathematical values. The loop will run as long as the variable error is False (There are two ways to write this answer.The loop will run as long as the variable progress_percent is less than 100%.The loop will run as long as the variable battery_power is greater than 0%.The loop will run as long as the variable stop_date is not true.The loop will run as long as the variable home_score is greater than the variable away_score.The loop will run as long as the variable day is “Wednesday”.The first question is given as an example: Let’s practice writing some conditions that we might set up for a WHILE loop.įor the questions below, write out the LOOP CONDITION only. If it evaluates to False, the loop will stop. If the condition evaluates to True, the loop will continue. The WHILE loop checks this condition each time it runs. The condition typically has 3 parts as shown in the diagram below:As we see above, the condition compares two values that will result in a True or False (A Boolean value). See the video below for how to write a WHILE loop and some examples of when we might use WHILE loops:Īs you can see, WHILE loops are often used to make our drones seem more intelligent by having them respond to their environments.Now that we understand what a WHILE loop is and how it works, let’s look at one of the more important parts, the condition. WHILE LOOPS: Use when we DON’T know how many times the loop will run. The steps that the loop will repeat until it is done.The most important difference between FOR and WHILE loops is their purpose:įOR LOOPS: Use when we have a set number of times we want the loop to run. Loop Body: What the loop will do or execute. Typically in the form of an inequality (for example i < 5). Loop Condition: What causes the loop to continue or to stop. ![]() Both FOR and WHILE loops have the same basic parts: In this lesson, we will learn about WHILE loops. In the previous lesson, we learned about loops, and more specifically, FOR loops.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |