May-05-2020, 01:01 AM
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
- 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
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) 16so 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