IT & IoT Security | Cloud | It's all about the life itself

Nothing in life is as important as you think it is, while you are thinking about it.

Python – Average word count

Hi  all, this a function usage sample and it aims to find average word count in a given string.  We call word_count and letter_count functions in average_word_length function. That’s all 🙂

def word_count(a_string):
    space_count = 0
    word_count = 0
    for i in a_string:
        if i == " ":
            space_count = space_count + 1
    word_count = space_count + 1
    return word_count

def letter_count(a_string):
    letter_count = 0
    for i in a_string:
        if i.isalpha() or i != " ":
            letter_count += 1
    return letter_count

def average_word_length(a_string):
    average_word_length = 0
    harf_sayisi = letter_count(a_string)
    kelime_sayisi = word_count(a_string)
    average_word_length = harf_sayisi/kelime_sayisi
    return average_word_length

a_string = "Up with the black and silver"
print("kelime sayisi",word_count(a_string))
print("harf sayisi",letter_count(a_string))

The output should be 3.8333333333333335

Leave comment

Your email address will not be published. Required fields are marked with *.