So what this would show here is that
Loops give us the count = 0
ability to the loops can repeat blocks of code
repeat blocks of based on certain conditions for an while count < 3:
code based on a exact number of times. print("I love python!")
certain condition, count += 1
or for an
exact number of While loop is similar and it would (they will count it less than 3 and 0
times. contain a certain condition on whether
and print i love python and will only
count 1 if it is + = and so that is
the code inside the loop will run or
The while loop what this code shows and on how it
not?
works very can be demoed for here)
similarly to If true then it will keep repeating
an if. while True:
A certain condition city = input("Please enter a city
controls whether name: (Nope to end) ")
the code inside of It will keep repeating checking the if city == "Nope":
it will run or not. condition each time after the code break
While the print("Oh! " + city + " is a cool
condition is true, spot.")
the code Four main parts of the loop all revolve
will keep repeating (if the code is true then it will input
around the control variable
- checking the the city to enter a name but it will
condition each not unless if a break and it will bring
time after the Oh!, city, is a cool spot and so that
code We initialize the control variable, is what this code would do.)
runs. create a while loop with condition
using control variable, code should The control variable needs to
his code will run if true, it would then update the be initialized before the loop.
output the word control variable for here. The keyword while
Hello 5 times: makes our while loop,
CS Python followed by our Boolean
Fundamentals — conditional: num < 5.
Unit 4
num = 0 Indented in the while loop is the
while num < 5: code that will
print("Hello") run - we print "Hello".
num += 1
num += 1 will increment our
There are four control variable by 1. This part
main parts of a keeps track of how many
while loop, and times the loop has iterated or
most of them repeated. After this line, we
revolve around loop back up to the while
what is known as and recheck our condition.
the control
,variable. # Initialize variables
total_sum = 0
The four basic count = 0
parts of our loop:
1. Initialize our print("Enter numbers to add to the
control variable running total.")
2. Create a while print("The program stops when the
loop with a sum exceeds 200.")
condition that
uses the control while total_sum <= 200:
variable try:
3. Code that # Prompt the user for input
should run while user_input = input("Enter a
the number: ")
condition is true
4. Update the # Convert input to a number
control variable (integer for this example)
number = int(user_input)
# Update the sum and the
count
total_sum += number
count += 1
except ValueError:
# Handle cases where the user
enters non-numeric input
print("Invalid input. Please
enter a valid integer.")
continue
# Output the results once the loop
condition (sum > 200) is met
print("") # Print a blank line for
readability
print("Sum:", total_sum)
print("Numbers Entered:", count)
name = input("Enter a name (STOP
Count Variable - Iterate = repeats to end): ")
Counts how many c=0
times the loop
runs or iterates. A count variable can check how while name != "STOP":
many times the loop runs or repeats c += 1
This is the code itself basically print("You entered " + name)
used in the name = input("Enter a name
example: (STOP to end): ")
, The code is used here and the name
name = always loops and runs, uses variable print("You entered " + str(c) + "
input("Enter a c to control the loop, and c+=x is names.")
name (STOP to used to go by increments of
numbers, After a name typed in this
end): ") (what this code would use would be
loop it will count the name and
c=0 reprompt them into that loop,and that it would show here that once
while name != STOP is obvious not to count it. you enter a name it will stop that
"STOP": name and make it different and so it
c += 1 would just stop the name in a way
print("You entered and so that is what this code shows
A sum variable is basically where it is
" + name) a count variable where it keeps a
here overall as a whole.
name = summary of how long they have ran
input("Enter a for and stuff and so that is what is And also it just shows as the name
name (STOP to shown here for the sum variable. was entered for here and so that is
end): ") what the result of this code would
print("You entered be.
" + str(c) + "
names.")
Name = loop runs
Variable c- is used
to control the loop
c+=1: goes by the
increments of the
names and is
needed to count
how many times a
user enters a
name
After they type a
name, it
enters the loop
and we
count that name.
Then we
reprompt them in
the loop.
STOP - not a
name and should
not be counted
c+=1: is at the
beginning of a
loop is because it