Bottom Page

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?
Quote
#2
since the iterator starts with zero, you need to add 1 to num
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Populating a timetable with subjects brittocj 1 193 May-02-2019, 07:00 AM
Last Post: buran
  Divisors shared the second numbers mircea_dragu 1 292 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  Populating Array2 from Array1 PappaBear 1 462 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  populating csv and searching the file mepyyeti 1 734 Apr-26-2018, 03:02 AM
Last Post: woooee
  for loop and list populating mepyyeti 3 1,016 Apr-12-2018, 03:06 AM
Last Post: buran
  Importing file in dataframe and populating missing column name vvv 1 1,341 Feb-26-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)