While statements provide a mechanism for repeating computations. The while statement has the form:while condition: <body_code><more_code>where condition is a boolean valued expressions and <body_code> is a sequences of statements.<body_code> is repeatedly evaluated until condition is broken (i.e. becomes false) in which case computation moves to <more_code>.When writing a while loop there are a few things to keep in mind:You will likely need to initialize one or more variables before the while statement.The condition (test) of the while statement will usually become false eventually (otherwise the loop will not terminate).The body code typically changes one or more variables in such a way that the condition will become false eventually.For example, the following code, when executed, will output a countdown. The variable count is first initialized, the condition tests if count has reached 0, and in the body of the loop count is decremented (and will eventually become 0).count = 5while count > 0: print(count) # initialize count = count - 1 # count will eventually descend to 0print("Thunderbirds are Go")TaskWrite a program that takes the average of an arbitrary number of floats. The number of floats and the floats themselves are all entered by the userDo this in the following way:Prompt the user to enter the number of floats to be entered. (This is in the starter code already.)Repeatedly prompt the user for those numbers using a while loop.Accumate the numbers into a running total and then print the average of the numbers.PreconditionsThe number of floats entered is an integer greater than zero.The subsequent floats are each greater than or equal to zero.
Question
While statements provide a mechanism for repeating computations. The while statement has the form:while condition: <body_code><more_code>where condition is a boolean valued expressions and <body_code> is a sequences of statements.<body_code> is repeatedly evaluated until condition is broken (i.e. becomes false) in which case computation moves to <more_code>.When writing a while loop there are a few things to keep in mind:You will likely need to initialize one or more variables before the while statement.The condition (test) of the while statement will usually become false eventually (otherwise the loop will not terminate).The body code typically changes one or more variables in such a way that the condition will become false eventually.For example, the following code, when executed, will output a countdown. The variable count is first initialized, the condition tests if count has reached 0, and in the body of the loop count is decremented (and will eventually become 0).count = 5while count > 0: print(count) # initialize count = count - 1 # count will eventually descend to 0print("Thunderbirds are Go")TaskWrite a program that takes the average of an arbitrary number of floats. The number of floats and the floats themselves are all entered by the userDo this in the following way:Prompt the user to enter the number of floats to be entered. (This is in the starter code already.)Repeatedly prompt the user for those numbers using a while loop.Accumate the numbers into a running total and then print the average of the numbers.PreconditionsThe number of floats entered is an integer greater than zero.The subsequent floats are each greater than or equal to zero.
Solution
Here is a Python program that fulfills the task:
# Prompt the user to enter the number of floats to be entered
num_of_floats = int(input("Enter the number of floats to be entered: "))
# Initialize a variable to hold the running total
total = 0.0
# Initialize a counter variable
counter = 0
# Use a while loop to repeatedly prompt the user for numbers
while counter < num_of_floats:
# Prompt the user for a number
num = float(input("Enter a float: "))
# Add the number to the running total
total += num
# Increment the counter
counter += 1
# Calculate the average
average = total / num_of_floats
# Print the average
print("The average of the numbers is", average)
This program first prompts the user to enter the number of floats they will input. It then initializes a total variable to 0.0 and a counter variable to 0. The while loop runs as long as the counter is less than the number of floats the user said they would input. Inside the loop, the program prompts the user to enter a float, adds that float to the total, and increments the counter. Once the loop has run the specified number of times, the program calculates the average by dividing the total by the number of floats, and then prints the average.
Similar Questions
Sometimes it is convenient to break out of a while loop from within the body of the while loop itself. This is often the case when it is more sensible to test a condition in the body that is different from the while loop condition.A typical case iswhile condition_for_doing: <some_code> if condition_for_breaking: break <more_code>If condition_for_breaking is satisfied then <more_code> is skipped:>>> x = 0>>> while x < 10:... print(f"Before {x}")... if x > 1:... break... print(f"After {x}")... x = x + 1... print(f"Done {x}")Before 0After 0Before 1After 1Before 2Done 2Note that breaking in the middle of a while loop is generally considered bad programming practice because it often leads to bugs which are difficult to resolve.Breaking from at the end of a loop to simulate a do-while loop is fine.TaskWrite a program that uses a while loop to repeatedly prompt the user for numbers until the user enters nothing (empty string). Print the average of the numbers entered.You must use a break statement to exit the while loop in order to credit for this question.
Why do we use while loops? To repeat code a specific number of times To use if-else statements in a loop To repeat code when the number of times to repeat is unknown To determine if a condition is true
What type of iteration is used in While Loop
Question 1In Python, what do while loops do?1 pointwhile loops tell the computer to repeatedly execute a set of instructions while a condition is true.while loops instruct the computer to execute a piece of code a set number of times.while loops branch execution based on whether or not a condition is true.while loops initialize variables in Python
In a while loop, what happens if the loop condition is always true?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.