DEV Community

Cover image for Python challenge_8
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python challenge_8

Counting syllables

level of challenge = 3/10

  • Define a function named count that takes a single parameter.
  • The parameter is a string.
  • The string will contain a single word divided into syllables by hyphens,
  • Such as these:
    "ho-tel"
    "cat"
    "met-a-phon"
    "ter-min-a-tor"

  • Your function should count the number of syllables and return it.

  • For example, the call count("ho-tel") should return 2.

Hint:

The trick is to realize that the number of syllables is the number of times the - symbol appears, plus one.
You'll have to count hyphens, then. Use a loop to iterate over each letter, or use the .count() method.

# My solution

def count(word):
    new_word = word
    count_syllables = new_word.split('-')
    return len(count_syllables)
print(count("ho-tel"))
Enter fullscreen mode Exit fullscreen mode
# Another solution

def count(word):
    syllables = 1
    for letter in word:
        if letter == "-":
            syllables = syllables + 1
    return syllables

# using the count method
def count(word):
    return word.count("-") + 1

# using split
def count(word):
    return len(word.split("-"))
Enter fullscreen mode Exit fullscreen mode

Add your solution in the comment :)

Top comments (1)

Collapse
 
alvisonhunter profile image
Alvison Hunter Arnuero | Front-End Web Developer • Edited
def count(word):
    return len(word.split('-'))
Enter fullscreen mode Exit fullscreen mode