Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 17 Online
OpenStudy (anonymous):

count_digits: (str) -> int Return the total number of characters in the given string that are digits. can anybody help me come up with a python code for this problem?

OpenStudy (anonymous):

I don't know if I get the question right, but for the number of characters just an "x = str(raw_input('ask: '))" and a "for" loop with an increasing variable.

OpenStudy (anonymous):

i need to count the number of digits within the string ignoring regular characters so if i wrote the word as kdfisfosdu32432 only the 32432 should be counted resulting in the answer 5 (as number of digits)

OpenStudy (anonymous):

Sorry for delay, should be something like this then?? x = raw_input("ingresa algo: ") ans = 0 for i in x: if (i == '0'): ans += 1 elif (i == '1'): ans += 1 elif (i == '2'): ans += 1 elif (i == '3'): ans += 1 elif (i == '4'): ans += 1 elif (i == '5'): ans += 1 elif (i == '6'): ans += 1 elif (i == '7'): ans += 1 elif (i == '8'): ans += 1 elif (i == '9'): ans += 1 print ans Is kinda rough, I'll try to make it simpler

OpenStudy (anonymous):

Going through it, I don't know how to make it simpler xD

OpenStudy (anonymous):

i dont think i can use that

OpenStudy (anonymous):

its too much aha

OpenStudy (anonymous):

hahaha it's kinda annoying I know, but you can make it a function :P

OpenStudy (anonymous):

i made this def count_digits(word): count = 0 for i in str(word): if i.isdigit(): count +=1 print (count) my only problem is say i do count_digits(ds23) instead of saying there is 2 digits it says count_digits(ds23) Traceback (most recent call last): File "<pyshell#81>", line 1, in <module> count_digits(ds23) NameError: name 'ds23' is not defined

OpenStudy (anonymous):

what can i do to fix that so i can put letters and numbers in the sam thing and the code will just register the count of numbers

OpenStudy (anonymous):

i know i have to make my variable word be a definitive string but how would i go about doing that?

OpenStudy (anonymous):

Have you made the function "isdigit()" ?? if is made you should do something like: if i == isdigit(i)

OpenStudy (anonymous):

Have you put any single quotes around 'ds23'?

OpenStudy (anonymous):

All are treated as strings, that's why you can't make something simple like this: for i in x: if type(i) == int: ans += 1

OpenStudy (anonymous):

you could do this def count_digits(word): count = 0 for c in word: for i in range(0,10): if str(i)==c: count +=1 print (count)

OpenStudy (anonymous):

http://dpaste.com/811789/

OpenStudy (anonymous):

msmithhnova the idea was without and "str()" xD

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!