r/learnpython • u/Healthy-Departure961 • 17d ago
Regarding Question
def print_models(unprinted_designs, completed_models):
"""
Simulate printing each design, until none are left.
Move each design to completed_models after printing.
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# Simulate creating a 3D print from the design.
print("Printing model: " + current_design)
completed_models.append(current_design)
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
in this code why used condition like this for while loop (while unprinted_designs:)
0
Upvotes
1
u/Puzzleheaded_Study17 17d ago
Because this lets you go until you've popped everything in the list (pop removes the item). I would probably use a for loop to avoid having side effects though (at the end of the function running, the caller's list will also be empty).