I dont understand, how?


#1
def count_small(numbers):
  total = 0
  for n in numbers:
    if n < 10:
      total = total + 1
  return total

lotto = [4, 8, 15, 16, 23, 42]
small = count_small(lotto)
print small

What I don’t understand is how can the line “if n < 10” reads the data inside lotto variable and determines which ones are under 10, INSTEAD OF actually counting the amount (or length) of data in the lotto variable which is 6?

Am I doing this wrong? haha


#2

There two things at play here, first the list named lotto which defined here:

lotto = [4, 8, 15, 16, 23, 42]

which you then pass as argument at function call:

# function call with argument lotto
small = count_small(lotto)

so the argument at function call get passed along to the function parameter (numbers)

The second part is the loop, for n in numbers. This will give us the values from the list, which we can check if they are less then 10, if so, increase total by one


#3

So um to confirm, the second part at play;

loop executes (or go through) each values in a list, and therefore the bot examines each value because it is going through due to the loop,

which otherwise the if n<10 would interpret the lotto input in a lengthy manner (interpret it as a list and examines its quantity instead of value) ?


#4

look:

lotto = [4, 8, 15, 16, 23, 42]
for value in lotto:
    print value

this will print each value of the list.

Now see what your code does

which bot?

Sorry, what? n contains the values from the list, so the values from the list are compares, length of the list is not relevant in this question


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.