Complete Python Series · Part 18 of 29
Assignment 05-storage-manager
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 Course Content Storage Manager
Learn to use while loops, sentinel values, break, continue, and running totals to manage a limited storage capacity.
Problem statement
NextGen CTO instructors upload course videos and learning resources to the platform. A simulated course storage allocation has a maximum capacity of 1000 MB. Build a program that accepts file sizes one at a time and tracks the total storage used. Stop when the instructor enters zero or attempts to upload a file that would exceed the capacity. Ignore negative file sizes.
Task requirements
- Initialize
max_capacityto 1000 andtotal_sizeto 0. - Use a
whileloop to repeatedly accept integer file sizes in MB. - If the file size is zero, stop the loop using
break. - If the file size is negative, skip it using
continue. - If adding the file would exceed the capacity, stop without adding it.
- Otherwise, add the file size to the running total and print a confirmation.
- Print the final total storage used after the loop ends.
Constraints
- Use a
whileloop, conditional statements,break, andcontinue. - Assume all inputs are valid integers.
- Do not use lists or functions.
- The total must never exceed 1000 MB. Exactly 1000 MB is allowed.
Good luck trying the exercise!
Starter code
max_capacity = 1000
total_size = 0
print(f"Starting course uploads. Capacity: {max_capacity} MB")
# TODO: Start a while loop
# TODO: Read the file size as an integer
# TODO: Break if the file size is zero
# TODO: Continue if the file size is negative
# TODO: Break if adding the file would exceed capacity
# TODO: Add the file size and print a confirmation
# TODO: Print the final total storage used
Expected output
Starting course uploads. Capacity: 1000 MB
Enter file size (MB): 400
File uploaded
Enter file size (MB): -50
Enter file size (MB): 350
File uploaded
Enter file size (MB): 300
Final storage used: 750 MB
Hints
- Use
while Trueto repeat until an explicit stopping condition is met. - Use
breakto exit a loop andcontinueto skip the current iteration. - Check
total_size + file_size > max_capacitybefore updating the total. - Use an f-string to display the final storage used.
Solution: NextGen CTO Course Content Storage Manager
The solution uses a sentinel-controlled loop and validates each file size before adding it to the running total.
max_capacity = 1000
total_size = 0
print(f"Starting course uploads. Capacity: {max_capacity} MB")
while True:
file_size = int(input("Enter file size (MB): "))
if file_size == 0:
break
if file_size < 0:
continue
if total_size + file_size > max_capacity:
break
total_size += file_size
print("File uploaded")
print(f"Final storage used: {total_size} MB")
Solution explanation
- Lines 1–2: Initialize the maximum storage capacity and running total.
- Line 4: Display the available capacity.
- Lines 6–7: Start an indefinite loop and convert each input into an integer.
- Lines 9–10: Treat zero as a sentinel value that terminates the loop.
- Lines 12–13: Ignore negative sizes by skipping the remaining statements in the current iteration.
- Lines 15–16: Check whether the next upload would exceed capacity before modifying the total. Exit the loop if it would.
- Lines 18–19: Update the running total and confirm the upload.
- Line 21: Display the final storage used after the loop terminates.
Test cases
This exercise simulates storage accounting only; it does not upload actual files.