Complete Python Series · Part 17 of 29
Assignment 04-server-load
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Server Load Monitoring System
Build a simple server monitoring program using for loops, integer inputs, comparison operators, and conditional statements. Learn to simulate infrastructure scaling decisions over five hours.
Problem statement
NextGen CTO needs to monitor its server infrastructure to ensure students can access courses without performance issues. Build a program that accepts CPU utilization readings for five consecutive simulated hours and recommends an action based on each reading.
Task requirements
- Print
Starting NextGen CTO Server Monitoring System.... - Use a
forloop to simulate five hours of monitoring. - During each iteration, collect CPU utilization using
input()and convert it to an integer. - Use
if,elif, andelseto determine the appropriate scaling action. - Print the corresponding action for each reading.
Constraints
- Use a
forloop withrange(). - Use integer values and standard comparison operators.
- Assume valid integer inputs between 0 and 100.
- Do not use external libraries or actual cloud infrastructure APIs.
Good luck trying the exercise!
Starter code
print("Starting NextGen CTO Server Monitoring System...")
# TODO: Write a for loop that iterates five times
# TODO: Capture CPU utilization and convert it to an integer
# TODO: Check whether CPU utilization is below 30 or above 75
# TODO: Print the appropriate scaling action for each case
Expected output
Starting NextGen CTO Server Monitoring System...
Enter CPU utilization (%): 20
Scale down servers
Enter CPU utilization (%): 85
Scale up servers
Enter CPU utilization (%): 45
Maintain current capacity
Enter CPU utilization (%): 75
Maintain current capacity
Enter CPU utilization (%): 15
Scale down servers
Hints
range(1, 6)generates five numbers, from 1 to 5.- Use
int(input(...))to capture integer values. - Use
<and>to compare CPU utilization with the thresholds. - The
elsebranch handles values from 30 to 75, including both boundaries. - Remember to indent the loop body and each conditional branch correctly.
Solution: NextGen CTO Server Load Monitoring System
The solution uses a for loop to repeat the monitoring process five times and an if-elif-else statement to select one scaling action for each CPU reading.
print("Starting NextGen CTO Server Monitoring System...")
# Simulate five hours of server monitoring
for hour in range(1, 6):
# Capture CPU utilization
cpu_usage = int(input("Enter CPU utilization (%): "))
# Determine the appropriate scaling action
if cpu_usage < 30:
print("Scale down servers")
elif cpu_usage > 75:
print("Scale up servers")
else:
print("Maintain current capacity")
Solution explanation
- Line 1: Print the system startup message once, before the loop begins.
- Line 4: Use
range(1, 6)to repeat the loop exactly five times. The stop value is exclusive. - Line 7: Capture a CPU reading and use
int()to convert the input string into an integer. - Lines 10–11: If CPU utilization is below 30%, recommend scaling down.
- Lines 12–13: Otherwise, if utilization is above 75%, recommend scaling up.
- Lines 14–15: For all remaining valid readings, maintain current capacity.
Test cases
The hours and thresholds are simulated for educational purposes. Actual production autoscaling uses trusted monitoring metrics, sustained load thresholds, capacity limits, and other safeguards.