## MichelleCrews

i have to write a function called number_of_2s that includes one parameter, N, where N is a positive integer that is no more than 10 digits long. the function should return the number of times the digit 2 occurs in the number (N) ... example number_of_2s(7822622) returns 4

this is what i have so far and i dont know where to go after this :(

def number_of_2s(N):
N = (N<==10)

## vegaseat 1,720

Hint ...

``````n = 123452267
# convert number to a string
s = str(n)
# count the number of '2' in the string
print s.count('2')``````

## MichelleCrews

Hint ...

``````n = 123452267
# convert number to a string
s = str(n)
# count the number of '2' in the string
print s.count('2')``````

thanks im going to try that ... but is there a way to do it without using a string? .. i think our teacher said we cant use a string

## lukerobi 4

i have to write a function called number_of_2s that includes one parameter, N, where N is a positive integer that is no more than 10 digits long. the function should return the number of times the digit 2 occurs in the number (N) ... example number_of_2s(7822622) returns 4

this is what i have so far and i dont know where to go after this :(

def number_of_2s(N):
N = (N<==10)

``````def number_of_2s(N):
return str(N).count('2')``````
``````>>> number_of_2s(223282882)
5
>>>``````

Make sure u can't use str() by emailing your prof.... it makes it harder and a little more difficult without it.

## MichelleCrews

``````def number_of_2s(N):
return str(N).count('2')``````
``````>>> number_of_2s(223282882)
5
>>>``````

Make sure u can't use str() by emailing your prof.... it makes it harder and a little more difficult without it.

yea i just checked with a friend and no string :( ... can u help me with it without using srting please

## vegaseat 1,720

Hint #2 ...

``````n = 123452267

count2 = 0
while True:
remainder = n % 10
if remainder == 2:
count2 += 1
# // gives quotient
n = n//10
print n, remainder  # for testing only
if n == 0:
break

print count2  # 3``````

## lukerobi 4

yea i just checked with a friend and no string :( ... can u help me with it without using srting please

yeah... hm... your basically reinventing the wheel with that request...

I don't know any simple method to get that done without using str(),

You could use this to find out how many digits your number has:

``````def howManyDigits(x):
y = 0
while x != 0:
x /= 10
y += 1
return y``````

Then u might be able to figure out how to subtract/divide/whatever to isolate each number and see if u can divide by 2 and get 0. It's not my homework, so im not going to do it... but there is probally some python tool i dont know about that will do this... i just have allways used str()

** edit

looks like someone else solved it better than i could... lol

## MichelleCrews

Hint #2 ...

``````n = 123452267

count2 = 0
while True:
remainder = n % 10
if remainder == 2:
count2 += 1
# // gives quotient
n = n//10
print n, remainder  # for testing only
if n == 0:
break

print count2  # 3``````

ok i know this is wrong but heres wha i got

def number_of_2s(n):
count2 = 0
while(n<=10):
remainder = n % 10
if remainder ==2:
count +=1
n = n//10
if n==0:
break
print "n"

i get an error about the "n"

## vegaseat 1,720

A number of glaring errors here, but your code block indentations are really wrong. Wrapping your code into code tags, this is what you have ...

``````def number_of_2s(n):
count2 = 0
while(n<=10):
remainder = n % 10
if remainder ==2:
count +=1
n = n//10
if n==0:
break
print "n"``````

Here is more or less what you should use ...

``````n = 123452267
print n

# check if n exceeds 10 digits
# 9999999999 is the highest 10 digit number
if n > 9999999999:
print n, "exceeds 10 digits"
else:
count2 = 0
while True:
remainder = n % 10
if remainder == 2:
count2 += 1
n = n//10
if n == 0:
break
print "total 2 count =", count2``````

## MichelleCrews

thanks man .... my problem is ... that i dont undastand this stuff so when she give us these homework assignments im LOST! so i start writen and dont even know what im tryna write lol im soooooooo a newbi to this programming thing sry !!