[Tutor] Need to discuss about a class that can be used to print number of vowels, consonants, uppercase and lowercase letters and spaces in a string

Alan Gauld alan.gauld at yahoo.co.uk
Thu Aug 19 13:08:15 EDT 2021


On 19/08/2021 15:54, Manprit Singh wrote:

> class StringStats:
>     def __init__(self, seq = ""):
>         self.str_seq = seq
>         self.cnt_lowercase = 0
>         self.cnt_space= 0
>         self.cnt_uppercase = 0
>         self.compute_stats()
> 
>     def compute_stats(self):
>         for ch in self.str_seq:
>             if ch == " ":
>                 self.cnt_space += 1
>             elif ch.isupper():
>                 self.cnt_uppercase += 1
>             elif ch.islower():
>                 self.cnt_lowercase += 1

> !) How can i place self.compute_stats() inside __init__ without
> placing all those instance variables that are initialized to 0.

You could put them into the compute_stats() method.
You need to set them to zero there anyway so you might
as well just initialize them there every time.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list