Python Forum

Full Version: Populating a list with divisors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I did Python Practice Exercise 4 from here:
https://www.practicepython.org/solution/...tions.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?
since the iterator starts with zero, you need to add 1 to num