I'm a rank beginner in Python, and I am working my way through various relevant OpenCourseware modules. In response to the prompt
Write a procedure that takes a list of numbers, nums, and a limit, limit, and returns a list which is the shortest preﬁx of nums the sum of whose values is greater than limit. Use for. Try to avoid using explicit indexing into the list.
I wrote the following simple procedure
for i in nums:
It gets the job done, but the division of labor between if and else seems inelegant, as does the use of both return and print. Is there a better way to structure this basic loop?