FOUNDATIONS OF PROGRAMMING : LOOPS AND FUNCTIONS : 03.03 WHILE LOOPS

Ace your homework & exams now with Quizwiz!

Using while Loops to Count: While Conditions with Strings

# The while Loop def main(): print("Let's find the total.") total = 0 answer = "y" while(answer == "y"): num = int(input("Please enter a number.")) print(num) total = total + num answer = input("Would you like to add another number? y/n ") print("Your total is " + str(total) + ".") main() Output: Let's find the total. 10 Your total is 10. The while loop condition can test numeric values and string values. Here, the while loop continues while the answer is y. To prevent an infinite loop, the user is asked if they want to continue. If the input is y, the loop will repeat again. The relational operator == checks if the value of answer is equal to y. You never know when the user will ask to stop. This loop could repeat one time or 100 times

Using while Loops to Count: Finding the total

# The while Loop def main(): total = 0 while(total < 20): num = int(input("Please enter a number.")) print(num) total = total + num print("Your total is " + str(total) + ".") main() Output: 7 7 8 Your total is 22. When we look at the condition for this while loop, we see it will continue while the total is less than 20. Since the user is inputting values, we don't know when the total will reach 20. The user may enter one value or 100 values. This makes while loops flexible to repeat until an event occurs. In this case, when the total is greater than or equal to 20, the while loop will stop and the total will be printed. Explore the code by making changes.

Using while Loops to Count: Using while to Count

# The while Loop def main(): x = 0 while(x < 5): print(x) x = x + 1 main() Output: 0 1 2 3 4 This while loop repeats while the value of x is less than 5. The initial value of x is 0. Since 0 is less than 5, the block of code within the while loop is executed. The value of x is printed, and then the value of x is updated to be x + 1. The last time through the loop, x is assigned the value 5, which is not less than 5, so the loop ends. If the value of x wasn't updated inside the loop, the condition would always be true and the loop would never end!

Using while Loops to Count: Making Roses

# The while Loop import turtle def main(): rosemary = turtle.Turtle() rosemary.speed(10) rosemary.color("purple") rosemary.penup() rosemary.setposition(0, 50) rosemary.pendown() numCircles = 10 x = 0 while(x < numCircles): rosemary.circle(40) rosemary.left(360 / numCircles) x = x + 1 main()

Using while Loops to Count: User input and continuing

# The while Loop import turtle def main(): trixie = turtle.Turtle() trixie.speed(10) answer = "y" while(answer == "y"): numCircles = int(input("How many circles should be drawn?")) diameter = int(input("What is the diameter for the circles?")) trixie.color(input("What color is the turtle?")) x = 0 while(x < numCircles): trixie.circle(diameter) trixie.left(360 / numCircles) x = x + 1 answer = input("Would you like to draw another? y/n ") main() This program has two while loops, one inside the other! Notice the indentation of each block of code within the while loops. Each block of code repeats while the condition is true. The user controls how many roses are drawn. The ending of the loop is dependent on how the user feels. We won't know how many times the loop will repeat because the user gets to decide. The while loop is waiting for the event of the answer to be something other than yes.

Boolean Conditions

Boolean Conditions Python uses what's known as Boolean logic to determine whether the condition is true or false. The loop will repeat the action as long as the condition provided is true. As soon as the condition becomes false, the loop will end. To make your while loop understand the conditions necessary for it to run, you will need to use one of the six relational operators. These will be pretty familiar if you know basic algebra, but sometimes the symbols are a little different. Algebra Programming Less than or equal to ≤ <= Less than < < Equal to = == Not equal to ≠ != Greater than > > Greater than or equal to ≥ >=

Did You Know?

It is possible to write infinite loops! These go on forever. For example: x = 2 while(x <= 2): print("It never ends!") In this code, you have not provided any information to make the condition become false. Your computer will probably freak out when it tries to run this! The body of the loop needs the code to change the value of x so that at some point in time, x is not less than or equal to 2. Thinking in Boolean conditions for while loops can be pretty tricky at first. The key is paying attention to detail and logic. Ask yourself what must be true for the loop to end. Then reverse that condition to decide what will be true while the loop is repeating. This backward method of thinking takes some getting used to, but it will help you write perfect loops before long!

Writing while loops

Writing while loops There are some important syntax points to be aware of when writing a while loop: The statement begins with the keyword while. The test condition goes inside the parentheses. The statement ends with a colon. The code in the body of the loop gets indented. Here's an example: answer = "y" while(answer == "y"): trixie.circle(25) trixie.left(25) answer = input("Would you like to draw another? (y/n) ") A while loop needs to know two main things: What action to do The conditions for doing this action

for loop

for loop This structure is commonly used to count through a range. It tells Python to repeat the action for a certain number of iterations. Use this loop when you know the exact number of times your code should run. Example: for x in range(0, 1, 10):

for loops and while loops are similar in that they are both loops, but they differ in why and how you use them.

for loops Repeat for a set range of values The loop variable is given a start value Best used when you know the exact number of repetitions needed while loops Repeats while a Boolean condition is true The loop variable is assigned a value before the loop Best used when you do not know the exact number of repetitions needed Common factors of for and while loops Repeat a block of code Will stop once the condition is false

Using while Loops to Count: Using while and for Loops

import turtle def main(): joe = turtle.Turtle() joe.speed(10) joe.shape("turtle") #Options: turtle, arrow, circle, square, triangle, classic joe.penup() joe.setposition(0, 30) answer = "y" while(answer == "y"): size = 1 joe.color(input("Please enter a color.")) joe.shape(input("Please enter a shape (turtle, arrow, circle, square, triangle, classic). ")) for i in range(15): joe.stamp() size = size + 2 joe.forward(size) joe.left(30) answer = input("Do you want to continue? y/n ") main() The while loop and for loop are both used in this program. The while loop will continue until the user's answer is something different than y. The for loop will count from 0 to 14. By combining loops, your programs become more powerful. When you want to repeat a section of code, use a loop. Make some changes to this code to see what happens.

while loop

while loop This command waits for an event to happen. It tells Python to repeat the action while a certain condition is true. Use this loop when you don't know the exact number of times you need your code to run. Example: x = 0 while(x < 10): x = x + 1


Related study sets

Identification and Authentication

View Set

Fluid & Electrolyte Adaptive Quizzing

View Set

N308 Chapter 5: Chronic Illness and Older Adults (Exam 1)

View Set

Legalities in Nursing Practice Questions

View Set

Protons, Neutrons, and Electrons

View Set

Exam III- Fossil Fuels and Nuclear Energy

View Set