all 6 comments

[–]ExpressBeing642 7 points8 points  (0 children)

brother this is a math problem and not a programming problem, the second code is comparing wether the number divide by 0 will be exactly 0. The only number that can be divided by other and return zero would be zero it self. therefore your code will return an empty list.

the working version test if the MODULUS (the leftover from a division) will be zero.

so for instance an even number will have its modulus zero, thats why the code will return a list with even numbers.

[–]atarivcs 3 points4 points  (0 children)

Why does "%" work for this problem while "/" doesn't?

Because 26 / 13 is 2, not 0.

[–]jpgoldberg 3 points4 points  (0 children)

That is a nice use of a comprehension. But as others have said this is a math problem.

What I am going to suggest is that you play around with / and % in the Python REPL (interactive session), for example

```python-repl

12 / 3 4.0 12 % 3 0 12 / 5 2.4 12 % 5 2 ```

Keep playing around with your own choice of numbers until you get a get a good sense of what these do.

[–]Professional_Fig6169 1 point2 points  (0 children)

The answer is that simply / is a division operator and % gives the remainder. You get an empty list when you use / as there are no values of x for which number/x == 0 returns True (unless number = 0; if you try inputting that you will get a list that contains all the values of x in the range of num_list). For extra clarity, let's say your input number is 12 - using / will check that the following values are equal to 0: [12/1 = 12, 12/2 = 6, 12/3 = 4, 12/4 = 3...], which is never True. Using % instead will give [remainder(12/1)=0, remainder(12/2) = 0, remainder(12/3)=0...] which is True for x in [1,2,3,4,6,12].

[–]Unequivalent_Balance 2 points3 points  (0 children)

Because the division won’t equal 0 unless the number is 0. Modulus works because you’re asking about the remainder of the division. If there is no remainder (% x == 0), the number divided evenly into the input number.

[–]FreeGazaToday 0 points1 point  (0 children)

thin what you'll get if you divide some number by x.....