0% found this document useful (0 votes)
3 views3 pages

While Loop Lab Manual

Uploaded by

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

While Loop Lab Manual

Uploaded by

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

Practicing the While Loop in Python

Topic: Using Loops for Repetition and Conditional Execution

Class Outcomes:
After completing this laboratory exercise, students will be able to:

 Use the while loop construct in Python to repeatedly execute a block of code based on a
condition.
 Apply conditional statements within a loop to control program flow.
 Implement basic input handling, accumulation, and iteration logic using a while loop.
 Enhance a while loop program with additional features such as sum, average, and count.

Introduction:
The while loop is one of Python’s most powerful control structures for repeating a set of
actions until a specified condition is no longer true. It is commonly used when the number
of repetitions is unknown in advance — such as continually accepting user input until a
certain value is entered. In this activity, you will learn how to use the while loop to
repeatedly ask the user for input, perform calculations, and manage program flow using
conditions. This exercise also introduces enhancements like calculating the total, average,
and count of numbers — essential techniques for data processing and control logic in
programming.

Materials:
 PC or Laptop with Python installed
 IDE (PyCharm, VS Code, or any text editor)
 Command prompt or terminal for program execution

Procedure:
1. Open PyCharm.
2. Write a Python program that uses a while loop to repeatedly ask the user to enter a
number until they enter a negative number.
3. Within the while loop:
- Prompt the user to enter a number.
- Check if the number is negative.
- If the number is negative, exit the loop.
- If the number is non-negative, display a message indicating that it is a non-negative
number.
4. Run the script to test if the program correctly stops when a negative number is entered.

Example code:
num = 0
while num >= 0:
num = int(input("Enter a number: "))
if num >= 0:
print("You entered a non-negative number.")

5. Modify the program to calculate the sum of the non-negative numbers entered. Initialize
a variable total_sum to 0 before the loop. Inside the loop, add each non-negative number
to total_sum. After the loop ends, print the total sum.

num = 0
total_sum = 0

while num >= 0:


num = int(input("Enter a number: "))
if num >= 0:
total_sum += num
print("You entered a non-negative number.")

print("The total sum of non-negative numbers is:", total_sum)

6. Add extra functionality to your existing program. You may choose one or more of the
following:
- Calculate the average of the non-negative numbers.
- Find the maximum or minimum value among the non-negative numbers.
- Count the number of non-negative numbers entered.

7. Run the modified script to observe the updated output.

Analysis:
1. How did you use the while loop construct to repeatedly execute a block of code based on
a condition?
2. How did you handle the input of numbers from the user within the loop?
3. What programming logic did you use to calculate the sum, count, or average?
4. What are some real-world situations where a while loop would be useful?
5. What challenges did you encounter while designing your loop, and how did you resolve
them?
Data:
Record the numbers entered during the execution of your while loop. Include both the non-
negative numbers and the final negative number that terminates the loop. This section
helps verify the accuracy of your loop condition and calculations.

Example Table:

Input Number Remarks

Number 1 Non-negative / Negative

Number 2 Non-negative / Negative

Number 3 Non-negative / Negative

Number 4 Non-negative / Negative

Number 5 Non-negative / Negative

Calculations:

Use this section to show how you computed the total sum, average, or other derived values
from your inputs. Demonstrate the mathematical logic that corresponds to your Python
program’s calculations.

Example:

If the user entered: 5, 3, 2, and then -1 to stop the loop:


Total Sum = 5 + 3 + 2 = 10
Count = 3
Average = Total Sum / Count = 10 / 3 = 3.33

Extension Activity:
Write a new version of the program that allows the user to stop the loop either by entering
a negative number or typing “exit.” Use string input handling and conditional logic to make
your program more flexible.

You might also like