Write a function count_digits (s) that counts the number of digits in the string s and returns answer (an integer).
The function must internally use a for-loop.


Example ;

>>> count_digits("1q2w3e4r5t")
5
>>> count_digits("42")
2
>>> count_digits("tea")

, Id appreciate any help:)

Edited 5 Years Ago by ericoscarj: n/a

We are awaiting for you code curiously, do not forget CODE button.

Edited 3 Years Ago by happygeek: fixed formatting

for my code? what do you mean, im really new to this and im having a really hard time to understand this problem, so i wonder if there is anyone that would like to help me with it. It would be really nice of you

Edited 3 Years Ago by happygeek: fixed formatting

Do it yourself and describe the process in words (that is called pseudocode).

Where you think for loop would fit in in that process?
How you think to recognize numbers?

It is effort for effort here. After we can build it in Python syntax, which sometimes is almost what you said when writing the pseudocode.

If it is too much to count the digits at beginning, prepare first definition of function which prints all characters in the parameter in separate lines. Then integrate the answer to two previous questions to that base.

first of all im pretty sure that i have to use the function

def count_digits(s) ##as function
and s.isdigit() for it too see if its a int or string, and maybe use s.isalpha() too.
I have no idea how to use a for loop, ive never done it before.

Edited 5 Years Ago by ericoscarj: n/a

here I'll give you an example of how you should show us your pseudo code, let's say I'm trying to count the number of words in a list that have the letter b in them:

I need a parameter or input
I need a count
I need the count to go up by one every time a word has a b in it
I need to print the final total of the count

with that I came up with:

>>> blist=['bob','the', 'builder']
>>> 
>>> def b_count(alist):
	count=0
	for word in alist:
		if 'b' in word:
			count+=1
	print(count)

	
>>> b_count(blist)
2

please note that it is better practice to use return than print

Edited 5 Years Ago by pyguy62: n/a

Thanks alot for your help pyTony and pyguy62, you gave me lots of ideas

This article has been dead for over six months. Start a new discussion instead.