hey, ima first time programmer and i no clue whats going on someone please help!!

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)

lol sad right? :( sry please help!

Recommended Answers

All 9 Replies

Hint ...

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

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

hey, ima first time programmer and i no clue whats going on someone please help!!

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)

lol sad right? :( sry please help!

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.

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

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

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

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"

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.