Python Forum
Populating a list with divisors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating a list with divisors
#1
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?
Reply
#2
since the iterator starts with zero, you need to add 1 to num
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating an array dynamically zxcv101 1 1,106 May-17-2022, 11:24 AM
Last Post: deanhystad
  Populating a timetable with subjects brittocj 1 1,776 May-02-2019, 07:00 AM
Last Post: buran
  Divisors shared the second numbers mircea_dragu 1 2,015 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  Populating Array2 from Array1 PappaBear 1 2,033 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  populating csv and searching the file mepyyeti 1 2,923 Apr-26-2018, 03:02 AM
Last Post: woooee
  for loop and list populating mepyyeti 3 3,404 Apr-12-2018, 03:06 AM
Last Post: buran
  Importing file in dataframe and populating missing column name vvv 1 3,401 Feb-26-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020