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

Page 1 of 1

## 9 Replies - 10752 Views - Last Post: 23 April 2012 - 06:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276478&amp;s=8d8229999c492ec130c285944251efa8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fllenangel95

Reputation: 0
• 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

• Blue Sprat

Reputation: 321
• 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?

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

### #3 fllenangel95

Reputation: 0
• 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

Simown, 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?

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.

### #4 Simown

• Blue Sprat

Reputation: 321
• 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
```

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

### #5 fllenangel95

Reputation: 0
• 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

Simown, 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
```

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!!!

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

### #6 fllenangel95

Reputation: 0
• 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
```

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

### #7 sepp2k

• D.I.C Lover

Reputation: 2201
• Posts: 3,397
• 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.

### #8 fllenangel95

Reputation: 0
• 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

sepp2k, 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?
>>>

### #9 sepp2k

• D.I.C Lover

Reputation: 2201
• Posts: 3,397
• 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.

### #10 fllenangel95

Reputation: 0
• 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!