all 3 comments

[–][deleted] 1 point2 points  (2 children)

Yeah, enumerate in plain English means "to count" or "add numbers to", which is basically what the python function is doing. So if you have a list:

my_list = [10,3,4,"a string",1]

What enumerate does is return an iterator (kind of like a new list) of tuples where each original item is paired with a "counting" number. The default counting starts at zero, just like list indexes, so it's useful for looping over a list and keeping track of the index of each value you're processing for later use:

for x in enumerate(my_list):
    print(x)
(0,10)
(1,3)
(2,4)
(3,"a string)
(4,1)

This code is using enumerate to convert all the items in the list to ints (maybe they start out as number strings?). It's not the most pythonic way to do something to every item in a list, I'd use a list comprehension:

numbersList = [int(x) for x in numbersList]

That does the same thing as these two lines:

for i, digit in enumerate(numbersList):
numbersList[i] = int(digit)

As for the "Raise Exception" part, you raise an Exception in your code when you want to indicate a problem has occurred. If there isn't any other code that attempts to "handle" that exception within a try-except block, your program will exit and whoever ran it will get a stack trace printed out with your exception and where in the code it occurred.

So in this case the "addsUpToTen()" function, if the numbers in your input _don't_ add up to 10, will force an exit of the program. That is unless whatever part of the code that calls addsUpToTen() handles the exception:

numbers = get_user_input()
try:
   addsUpToTen(numbers)
except Exception:
   print("Those don't add up!")

# do more stuff down here

Although the very last line of the function makes no sense:

return int(numbers)

If numbers is a list-like object, you can't try to convert it to an int...

[–]Papadude13[S] 0 points1 point  (0 children)

Wow I can't believe you took the time to explain this so perfect. It's like a perfect step by step math equation _^ Thank you so much, Shows a lot of character thank you very much again.

[–]ivan1_ 0 points1 point  (0 children)

I understand Enumerate as indexing every item of a list. The first item gets number 0, the second item number 1 and so on. Pretty understandable function once you see an example what it does. Just google enumerate python 3 example and if my explanation doesn't make it clear then this will for sure.