Python Forum
Counting Vowels in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting Vowels in Python
#1
Hey friends, I'm trying to do a Python course on Coursera, but I just can't pass one of the assignments because I am so confused by the following question. I am about as new to programming as you can possibly get, so feel free to talk to me like an idiot. The question is:


Two function definitions are saved in the same file:
- A function count_vowels has one parameter, a word, and returns the number of vowels in that word
- A function count_consonants has one parameter, a word, and returns the number of consonants in that word

To determine the number of letters in a word, write a one-body line for the following function that calls both count_vowels and count_consonants

def count_letters(word):
    """ (str) -> int

    Return the number of letters in word.
    >>> count_letters('hello')
    5
    >>> count_letters('bonjour')
    7
    """ 
    # Write the one-line function body that belongs here.
The only example of defining a function they've given us in the course is this:

def square_root(num):
    return num**2

num=4
square_root(num)
16
so I understand that we have to return something, and I understand that we probably have to convert the string to int or float so we can do math with it, but otherwise I have no idea how to figure this out. Here are some of my questions about it:
- Were count_vowels and count_consonants defined earlier in the code & not shown here? If so, how & when?
- Do we need to do something like input("Enter Word:", word) to get the word? Otherwise, where would the word come from?
- What would the full code look like, that can count letters, identify vowels, and identify consonants?
- Most pressing of all, what the heck could i possibly fit in that one line that returns so many things?

Anyway, I'm sure this is basic enough to get me laughed out of the metaphorical room, but if you're reading this now, thanks so much for your help! No one responds on the Coursera forums, and I don't have any teachers or mentors in real life to ask. Thanks friends <3
Reply
#2
you are overthinking. As per the question statement count_vowels and count_consonants already exists in the same file (what does this tell you?). You need to write function that calls both count_vowels and count_consonants. The boilerplate for the function is provided. So you need to write one line that will return total number of letter in a word by calling the 2 functions already availablle
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug How to print only vowels from an input? PP9044 8 7,427 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Return not vowels list erfanakbari1 2 2,650 Mar-26-2019, 11:37 AM
Last Post: perfringo
  Help with finding vowels nlord7 1 2,230 Feb-27-2019, 04:40 AM
Last Post: ichabod801
  Functions to remove vowels and extra blanks RiceGum 10 5,784 Nov-17-2017, 05:40 PM
Last Post: heiner55
  Print the index of the vowels in a string MeeranRizvi 4 14,593 Dec-29-2016, 02:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020