0% found this document useful (0 votes)
2 views5 pages

Python Loop

The document provides examples of Python loops that react to changing conditions, specifically a while loop that responds to user input and a for loop that reacts to data values. The while loop allows users to increase, decrease, or display a counter based on their choices, while the for loop evaluates a list of numbers and prints whether each number is positive, negative, or zero. Both examples illustrate how loops can adapt their behavior based on external inputs or internal states.

Uploaded by

ssindhiya1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Loop

The document provides examples of Python loops that react to changing conditions, specifically a while loop that responds to user input and a for loop that reacts to data values. The while loop allows users to increase, decrease, or display a counter based on their choices, while the for loop evaluates a list of numbers and prints whether each number is positive, negative, or zero. Both examples illustrate how loops can adapt their behavior based on external inputs or internal states.

Uploaded by

ssindhiya1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python “reacting” loops (loops that respond

to changing conditions)
Example 1: A while loop reacting to user input
python
# Simple reacting loop: a small command menu

# 1. Initialize a variable to keep the loop running


running = True

# 2. Initialize some state the loop will react to


counter = 0

# 3. Start the loop that will keep reacting until we


decide to stop
while running:
print("\nMenu:")
print("1. Increase counter")
print("2. Decrease counter")
print("3. Show counter")
print("4. Quit")

# 4. Read user choice


choice = input("Enter your choice (1-4): ")
# 5. React based on the choice
if choice == "1":
counter += 1
print("Counter increased.")
elif choice == "2":
counter -= 1
print("Counter decreased.")
elif choice == "3":
print(f"Current counter value: {counter}")
elif choice == "4":
running = False # This will stop the loop next
time the condition is checked
print("Exiting...")
else:
print("Invalid choice, please try again.")
Step-by-step explanation
 Line: running = True You create a boolean variable
that controls whether the loop should keep going.
As long as running is True, the loop continues.
 Line: counter = 0 This is the state your loop will
react to. The user’s choices will change this value.
 Line: while running: This starts a while loop.
Before each iteration, Python checks the condition
running.
o If running is True, the body of the loop runs.
o If running becomes False, the loop stops.
 Menu print lines These lines show the user what
actions are available. They run every time the loop
repeats, so the menu “reacts” by reappearing after
each action.
 Line: choice = input("Enter your choice (1-4): ")
input() pauses the program and waits for the user
to type something and press Enter. Whatever the
user types is stored as a string in choice.
 if choice == "1": block
o If the user types "1", you increase counter by
1 with counter += 1.
o Then you print feedback so the user sees the
effect.
 elif choice == "2": block
o If the user types "2", you decrease counter by
1.
o Again, you print a message to show what
happened.
 elif choice == "3": block
o If the user types "3", you show the current
value of counter.
o The loop is “reacting” by reporting the current
state.
 elif choice == "4": block
o If the user types "4", you set running = False.
o On the next check of the while running
condition, it will be False, so the loop ends.
o You also print "Exiting..." to inform the user.
 else: block
o If the user types anything other than "1", "2",
"3", or "4", this block runs.
o You tell the user the input was invalid, and
then the loop repeats, giving them another
chance.
This is a classic “reacting loop”: it keeps running,
watches what the user does, and changes behavior and
internal state accordingly.

Example 2: A for loop reacting to data values


Here’s a shorter example where a for loop reacts to
each item in a list:
python
numbers = [3, -1, 0, 7, -5, 2]

for n in numbers:
if n > 0:
print(f"{n} is positive")
elif n < 0:
print(f"{n} is negative")
else:
print(f"{n} is zero")
Step-by-step explanation
 Line: numbers = [3, -1, 0, 7, -5, 2] You define a list
of integers. The loop will “react” differently
depending on each number.
 Line: for n in numbers: This starts a for loop. It
goes through the list one element at a time.
o On each iteration, n becomes the next value in
numbers.
 if n > 0: If the current number is greater than 0,
you print that it’s positive.
 elif n < 0: If the current number is less than 0, you
print that it’s negative.
 else: If it’s neither greater nor less than 0, it must
be exactly 0, so you print that.
Here, the loop is “reacting” to the data instead of user
input—different branches run depending on the value
of n.

You might also like