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")
>>> count_digits("42")
>>> count_digits("tea")

, Id appreciate any help:)

Recommended Answers

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

Jump to Post

Note also the .isdigit() method -

>>> 'd'.isdigit()
>>> '1'.isdigit()
Jump to Post

All 8 Replies

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

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

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.

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):
	for word in alist:
		if 'b' in word:

>>> b_count(blist)

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

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

Member Avatar

Note also the .isdigit() method -

>>> 'd'.isdigit()
>>> '1'.isdigit()
Be a part of the DaniWeb community

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