Welcome to programming :) I did not understand your question first, then I realized that python might be your first language. In that case congratulations on picking a very nice language as your first language.
Your question seems to ask for the list which is the shortest prefix of nums the sum of which is greater than the limit. Here, you might notice that it does not care about the intermediate values. Alls that the function asks is that the return value be greater than the limit. That is, this should be the output
>>> numberlist([1,2,3,4,5], 5)
No output in between. So for that goal, you need to remove the print statement in your code, and without the print, there is no need for the else. In languages like python, it is not required that there be an
else section to an if-else conditional. Hence you can omit it. We can also use enumerate to iterate on both index and the value at index. Using all these we have,
for index,i in enumerate(nums):
sum += i
Note that if you are unsatisfied with omitting the else part, you can turn it back by using
Note also that I used an array slice notation
nums[:index+1] that means all the values from
0 to index+1 in the array
This is a rather nice for loop. If you are feeling more adventurous, you might want to look at list comprehensions. That is another way to write these things without using loops.
edit: corrected for enumeration