Python Forum
Populating a list with divisors - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Populating a list with divisors (/thread-12336.html)



Populating a list with divisors - RedSkeleton007 - Aug-21-2018

I did Python Practice Exercise 4 from here:
https://www.practicepython.org/solution/2014/03/05/04-divisors-solutions.html

The resulting code is:
#!/usr/bin/env python3
#PracticePythonExercise04.py

num = int(input("Enter a number to find divisors for: "))

#listRange = list(range(1,num))
listRange = list(range(1,num+1))

divisorList = []

for number in listRange:
    if num % number == 0:
        divisorList.append(number)

print(divisorList)
What I don't understand is why we need the +1 on line 7? Why won't the commented-out statement on line 6 work?


RE: Populating a list with divisors - Larz60+ - Aug-21-2018

since the iterator starts with zero, you need to add 1 to num