DEV Community

Discussion on: Learning Python- Basic course: Day 4, The for loop

Collapse
 
aatmaj profile image
Aatmaj

Glad you are solving it. Let me explain in more depth.

Bug one. Range begins from zero.

The range can;t begin from zero. Because if it does, then the first value of i will be 0, and integer division be zero is not possible. This will result in the following error

Enter a number 10

Traceback (most recent call last):

  File "main.py", line 3, in <module>

    if n%i==0:

ZeroDivisionError: integer division or modulo by zero
Enter fullscreen mode Exit fullscreen mode

Bug two: while loop must end with n+1 and not n

LEt us say that we consider the input 10. The last value of the i will be 9 and not 10. Hence the result will be

Enter a number 10
1
2
5
Enter fullscreen mode Exit fullscreen mode

But we want the number 10 to be included in the set of divisors. Hence we put n+1 at the ending bracket.


Have you understood it now? If not please feel free to ask any doubts.