Reputation: 49
In the code below Question 13a asks me to have the function count how many vowels are in a string. (I don't have to call that function in my homework.) But I called it to test it out and that part is completely correct and it works. The string can be both uppercase and lowercase with NO punctuation.
Question 13b asks to create a dictionary. The key is the word in a string (the string has multiple words). The value is how many vowels in that individual word. The question is asking this: If the word has AT LEAST i amount of vowels, then append it to the dictionary (The word with the amount vowels) This function has two parameters. The first one is a string with NO punctuation. The second parameter represents the number of how many vowels the word MUST have to be appended to the dictionary. The professor wants me to call Function 13a this function as part of the algorithm. That being said, the output of Question 13a is the value of the key (the individual word) in this problem. I am having trouble with this question, because I just can't get Python to append the output of 13a (the number of vowels for a word) to the dictionary key.
And also in the code below, I did not work on the part yet where I was supposed use the variable i.
Here is my code:
print("Question 13a")
def vowelCount(s):
vowels = 'aeiou'
countVowels = 0
for letter in s.lower():
if letter in vowels:
countVowels += 1
print(countVowels)
print("Question 13b")
def manyVowels(t, i):
my_string = t.split()
my_dict = {}
for word in my_string:
number = vowelCount(word)
my_dict[word].append(number)
print(my_dict)
print(manyVowels('they are endowed by their creator with certain unalienable rights', 2))
If you cannot understand the question then here is the professor's directions:
Question 13a (10 points) The letters a, e, i, o and u are vowels. No other letter is a vowel. Write a function named vowelCount() that takes a string, s, as a parameter and returns the number of vowels that s contains. The string s may contain both upper and lower case characters. For example, the function call vowelCount('Amendment') should return the integer 3 because there are 3 occurrences of the letters 'A' and 'e'.
Question 13b (10 points) Write a function named manyVowels() that takes a body of text, t, and an integer, i, as parameters. The text t contains only lower case letters and white space. manyVowels() should return a dictionary in which the keys are all words in t that contain at least i vowels. The value corresponding to each key is the number of vowels in it. For full credit, manyVowels() must call the helper function vowelCount() from Question 11a to determine the number of vowels in each word. For example, if the input text contains the word "hello", then "hello" should be a key in the dictionary and its value should be 2 because there are 2 vowels in "hello". Input: 1. t, a text consisting of lower case letters and white space 2. i, a threshold number of vowels Return: a dictionary of key-value pairs in which the keys are the words in t containing at least i vowels and the value of each key is the number of vowels it contains. For example, the following would be correct output.
text = 'they are endowed by their creator with certain unalienable rights'
print(manyVowels(text, 3))
{'certain': 3, 'unalienable': 6, 'creator': 3, 'endowed': 3}
Upvotes: 0
Views: 3592
Reputation: 535
Your code needs some adjustments:
The first function should return a value not print it:
return (countVowels)
The second function is not adding the key with accompanying value to the dictionary correctly. You should use:
my_dict[word] = number
return {k:v for k, v in my_dict.items() if v > i}
Upvotes: 0
Reputation: 22420
def vowelCount(s):
num_vowels=0
for char in s:
if char in "aeiouAEIOU":
num_vowels = num_vowels+1
return num_vowels
def manyVowels(text, i):
words_with_many_vowels = dict()
text_array = text.split()
for word in text_array:
if vowelCount(word) >= i:
words_with_many_vowels[word] = vowelCount(word)
return words_with_many_vowels
print(vowelCount('Amendment'))
text = 'they are endowed by their creator with certain unalienable rights'
print(manyVowels(text, 3))
Output:
3
{'creator': 3, 'certain': 3, 'endowed': 3, 'unalienable': 6}
Try it here!
Upvotes: 0
Reputation: 897
Add a condition to add only words with enough vovels
def vowelCount(s):
vowels = 'aeiou'
countVowels = 0
for letter in s.lower():
if letter in vowels:
countVowels += 1
return countVowels
def manyVowels(t, i):
my_string = t.split()
my_dict = {}
for word in my_string:
number = vowelCount(word)
if number >= i:
my_dict[word] = number
return my_dict
The line my_dict[word] = number
adds the resuld of vowelCount(word)
to your dictionary. But only if the number of vovels is at least i
.
Upvotes: 1