9 Replies - 7448 Views - Last Post: 23 April 2012 - 06:45 PM Rate Topic: -----

#1 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 02:44 PM

I am taking a beginners programming class and am working on an assignment but for some reason I just can't figure this problem out. He wants us to write a function that given a list of numbers, counts the values in the sequence that are divisible by 3 (and returns the count). I just can't figure out how to right it so that it skips other numbers.
Is This A Good Question/Topic? 0
  • +

Replies To: Write a function that counts the values can be divided by 3

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 02:50 PM

As not to immediately give the answer to your question - rather you work it out. Do you know how to:

a ) Loop through a list of numbers?
b ) Check a single number divides by 3?

If you can work that out, you'll have your answer!

Even more help in pseudocode:

for every item in the list:
    if the item divides by 3:
         add the item to the count
    otherwise do nothing


This post has been edited by Simown: 23 April 2012 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 02:55 PM

View PostSimown, on 23 April 2012 - 02:50 PM, said:

As not to immediately give the answer to your question - rather you work it out. Do you know how to:

a ) Loop through a list of numbers?
b ) Check a single number divides by 3?

If you can work that out, you'll have your answer!

Even more help in pseudocode:

for every item in the list:
    if the item divides by 3:
         add the item to the count
    otherwise do nothing




I don't know how to do much I am trying to figure it out on my own. my professor will start explaining and then decide to go start talking about something else so I get quite confused.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 03:01 PM

I know you are trying to figure it out on your own, I am trying to help you!

So the answer to both of them questions is "no"?

Ok, we have a list of numbers:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

And now we want to go through them:

# For each number in our list a
for number in a:
   # print out the number
     print(number)
....
1
2
3
4
5
6
7
8
9


Now the second problem, to divide by 3 - this can be achieved with simple if statements:

# % (modulus) is the remainder we get if we divide one number by the second
if(6 % 3 == 0):
  print("It divides by 3!")
else:
  print("It doesn't divide by 3")
...
It divides by 3!


And again:

if(11 % 3 == 0):
  print("It divides by 3!")
else:
  print("It doesn't divide by 3")
...
It doesn't divide by 3

That should help you well on your way now, right? :)

This post has been edited by Simown: 23 April 2012 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 05:02 PM

View PostSimown, on 23 April 2012 - 03:01 PM, said:

I know you are trying to figure it out on your own, I am trying to help you!

So the answer to both of them questions is "no"?

Ok, we have a list of numbers:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

And now we want to go through them:

# For each number in our list a
for number in a:
   # print out the number
     print(number)
....
1
2
3
4
5
6
7
8
9


Now the second problem, to divide by 3 - this can be achieved with simple if statements:

# % (modulus) is the remainder we get if we divide one number by the second
if(6 % 3 == 0):
  print("It divides by 3!")
else:
  print("It doesn't divide by 3")
...
It divides by 3!


And again:

if(11 % 3 == 0):
  print("It divides by 3!")
else:
  print("It doesn't divide by 3")
...
It doesn't divide by 3

That should help you well on your way now, right? :)



Okay I put all of this in and all of this makes sense now I am just having a hard time combining it all right now I have this..


a = [ "1, 2, 3, 4, 5, 6, 7, 8, 9"]
count = 0

# For each number in our list a
for number in a:
    if (a%3 ==0):
       count = count + a
    else:
       count = 0



am I very far off? I know its not right because i get this message:





Traceback (most recent call last):
File "C:/Python27/sd", line 6, in <module>
if (a%3 ==0):
TypeError: unsupported operand type(s) for %: 'list' and 'int'
>>>

MOD EDIT: When you post code...ESPECIALLY PYTHON CODE...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 23 April 2012 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 05:33 PM

a = ["1,2,3,4,5,6,7,8,9,"]

def sum(a):
    total = 0
    for value in a:
        if (a/3 == 0):
            total += value
        return total




I have this now and I am not getting an error message but when I run it, it says restart then gives me 2 blank things

This post has been edited by JackOfAllTrades: 23 April 2012 - 06:02 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 05:52 PM

Using % was right, don't use /. The problem were your operands. You wrote a % 3 == 0, which asks "Is the remainder of dividing a by 3 equal to 0?" or in other words "Does 3 divide a?". But what is a? It's a list. So you ask whether a list is divisible by 3, which doesn't make sense. That's what python is trying to tell you by saying that list is an unsupported operand type for %. What you really want to know of course is whether number is divisible by 3. So ask that.
Was This Post Helpful? 0
  • +
  • -

#8 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 06:05 PM

View Postsepp2k, on 23 April 2012 - 05:52 PM, said:

Using % was right, don't use /. The problem were your operands. You wrote a % 3 == 0, which asks "Is the remainder of dividing a by 3 equal to 0?" or in other words "Does 3 divide a?". But what is a? It's a list. So you ask whether a list is divisible by 3, which doesn't make sense. That's what python is trying to tell you by saying that list is an unsupported operand type for %. What you really want to know of course is whether number is divisible by 3. So ask that.



Now its telling me that:



Traceback (most recent call last):
File "C:/Python27/l", line 6, in <module>
if(number%3 ==0):
TypeError: not all arguments converted during string formatting



is it something really little that i'm missing here or am i way off?
>>>
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 06:35 PM

That error message is telling you that number is a string and not a number. And now that I look at your code again I see why: You added quotes around the numbers in a. That makes a a list containing one string. I don't know why you did that, but it doesn't do whatever you intended it to do. So undo it.
Was This Post Helpful? 0
  • +
  • -

#10 fllenangel95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-April 12

Re: Write a function that counts the values can be divided by 3

Posted 23 April 2012 - 06:45 PM

Thanks I got it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1