7 Replies - 2195 Views - Last Post: 21 September 2012 - 06:43 AM Rate Topic: -----

#1 luvmybbyjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 12

How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:31 AM

I am very new to python, actually I'm a beginner in programming. I'm having such a hard time to get this code right.
I need to print a list of the three-digit numbers that are divisible by 17, and print how many there are in total. This is what I have so far.
I'm sure some of is right, but I get an error in line 7
TypeError: 'int' object is not iterable


print ("How many three-digit numbers are divisible by 17?")
input("Press Enter to find out...")
list_of_ints = []

for number in range(100,1000):
    if number % 17 == 0:
         list_of_ints += number #line 7 error
         
print(list_of_ints)        
print(len(list_of_ints))
    



Any help or suggestions please?

I'm using Python 3

Is This A Good Question/Topic? 0
  • +

Replies To: How many three digit numbers are divisible by 17?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:32 AM

Using the += operator on lists can be quirky. Prefer append:
list_of_ints.append(number)



Also, investigate Python list comprehensions. You can do that in one line, believe it or not. ;)
Was This Post Helpful? 1
  • +
  • -

#3 luvmybbyjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 12

Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:35 AM

Thank you very much. This really helped a lot, "append" worked.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 11:22 AM

Your initial code was great, much better than what I expect to see from people just starting out, good job :)

I thought the assignment was kind of a fun one, so I decided to take a whack at it. Here it is:
def get_multiples(minimum, maximum, multiple):
    if minimum > maximum:#if minimum is bigger than maximum, swap their values
        minimum,maximum = maximum,minimum
    results = []
    value = minimum + (multiple - (minimum % multiple))
    while value < maximum:
        results.append(str(value))
        value+=multiple
    print("All "+str(len(results))+" multiples of "+str(multiple)+" between "+str(minimum)+" and "+str(maximum)+": "+", ".join(results))




this one is exclusive, which means that if you want all the multiples of 17 from 100 to 999, you'd say check(100,1000,17). I store them as strings so that I can use the join function :)
Was This Post Helpful? 0
  • +
  • -

#5 luvmybbyjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 12

Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 12:08 PM

Thanks, but it took me a while to get that code. I knew I had to get a list and the total amount of numbers just wasn't sure how, fortunately I got it.

That's a little confusing to me still but thanks. :)
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 09:32 PM

Hehe, I know my code uses some techniques you might not be familiar with, I'd be happy to discuss any parts you'd like more info on if you're curious :)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How many three digit numbers are divisible by 17?

Posted 21 September 2012 - 02:12 AM

Well, since we're on to alternatives...

This would get you your list:
list_of_ints = [ n for n in range(100,1000) if n % 17 == 0 ]



However, a more efficient way to get this list would be start at the first value in the list and increment by the value. This is what atraub did and could be one of the source for confusion.

d = 17 # divisor
start = 100 # bottom threshold
offset = start % d # this is how many we're off
offset = d - offset # the inverse of how much we're off
start = start + offset # first value we can use
list_of_ints = [ n for n in range(start,1000,d) ] # loop, skipping by divisor



Now, if you're just looking for the count...
def threeDigitsDivdedBy(d):
	return 999/d - 99/d


Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: How many three digit numbers are divisible by 17?

Posted 21 September 2012 - 06:43 AM

Ahh damnit!! I had the same idea while I was laying in bed this morning and you beat me to it!

EDIT:
def get_multiples(minimum, maximum, multiple):
    if minimum > maximum:#if minimum is bigger than maximum, swap their values
        minimum,maximum = maximum,minimum
    minimum = minimum + (multiple - (minimum % multiple))
    return [i for i in range(minimum,maximum,multiple)]



To do it in one line (assuming minimum is less than maximum):
results = [i for i in range((minimum + (multiple - (minimum % multiple))),maximum,multiple)]

This post has been edited by atraub: 21 September 2012 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1