## 9 Replies - 21645 Views - Last Post: 23 April 2012 - 06:45 PM

### #1

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

Posted 23 April 2012 - 02:44 PM

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

### #2

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

Posted 23 April 2012 - 02:50 PM

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

### #3

## 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:

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.

### #4

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

Posted 23 April 2012 - 03:01 PM

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

### #5

## 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:

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

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

### #6

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

### #7

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

Posted 23 April 2012 - 05:52 PM

*number*is divisible by 3. So ask that.

### #8

## 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:

*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

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

Posted 23 April 2012 - 06:35 PM

### #10

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

Posted 23 April 2012 - 06:45 PM