Reputation: 37
I am assuming that it has something to do with my lack of a return function, but everything I've tried comes back with errors, which makes me think there is something more going on. Also, I'm sure there's a much easier way to do this test and any suggestions are greatly appreciated. Thank you in advance for your patience and help as I'm completely new to this and learning. The print statement reads as follows:
aaaaNone
aaaaiaAeiaoaiaaiieeiaeuiaaeouaueaoaAiououeaeaEeooeoiNone
So, why the "None"s?
Given by professor:
def string_of_vowels(str):
"""
04: Return a string of all the vowels in a string.
Input str is a String.
Your function should return a string of vowels in this string, in the sequence they appear, including the duplicates.
For example, if the input parameter is "Casablanca", the return value should be "aaaa".
Be careful: you should count the vowels a, e, i, o, u and their upper letters
"""
My coding:
x = str
i = 0
while i < len(x):
if x[i] in ["a"]:
print ("a", end="")
elif x[i] in ["A"]:
print ("A", end="")
elif x[i] in ["e"]:
print ("e",end="")
elif x[i] in ["E"]:
print ("E", end="")
elif x[i] in ["i"]:
print ("i", end="")
elif x[i] in ["I"]:
print ("I", end="")
elif x[i] in ["o"]:
print ("o", end="")
elif x[i] in ["O"]:
print ("O",end="")
elif x[i] in ["u"]:
print ("u",end="")
elif x[i] in ["U"]:
print ("U",end="")
i += 1
Given by professor:
# test for Q4
print(string_of_vowels('Casablanca'))
print(string_of_vowels('''Casablanca is a 1942 American romantic drama film directed by Michael Curtiz and based on Murray Burnett and Joan Alison's unproduced stage play Everybody Comes to Rick's. '''))
Upvotes: 1
Views: 71
Reputation: 2137
This answer is a bit long, but take some time to read it. It seems that you already almost have a solution to the problem, so I will give a simpler one here for you to see the beauty of Python, and to show you maybe how to think slightly differently on a problem like this.
in
operatorYou seem to already know what in
does. x in y
will return true if x
is in the sequence y
. I'm using "sequence" here because y
can actually be any list, tuple, string, (or iterable). So, if you want to check if a character is in a string, you can simply use in
. For example, 'c' in 'acorn'
returns true, because 'acorn'
has a 'c'
in it. Case matters here.
Now that we know that the in
operator can work with strings, we can simplify your function a bit. If you want to check if x
is a vowel, you can do: x in 'aAeEiIoOuU
, right? Much simpler.
for
loopYou use a while loop in your answer, with an index that you look up each time, but a simpler solution would be to iterate each of the characters in the string, like so
for c in str:
if c in 'aAeEiIoOuU':
# c is a vowel here
Now, we can start keeping track of the vowels, by adding only the vowels together, like so:
vowels = ""
for c in str:
if c in 'aAeEiIoOuU':
vowels += c
Here, vowels += c
means vowels = vowels + c
, which concatenates the strings. For example, "aeio" + "u"
results in "aeiou"
.
At this point, vowels
contains all of the vowels in str
, and now all we need to do is return vowels
from the function. Returning is different then printing, in the sense that printing is like showing someone an image of a cake, while returning is giving someone an actual cake.
So, our function ends up looking like this
def string_of_vowels(str):
vowels = ""
for c in str:
if c in 'aAeEiIoOuU':
vowels += c
return vowels
Upvotes: 0
Reputation: 109
It's not wrong.Because you did not give the return value, will return to none.You can modify the way you call,like this:
string_of_vowels('Casablanca')
string_of_vowels('''Casablanca is a 1942 American romantic drama film directed by Michael Curtiz and based on Murray Burnett and Joan Alison's unproduced stage play Everybody Comes to Rick's. ''')
then will not found 'none',or return a string for function to replace 'print'.
Upvotes: 0
Reputation: 16
In the function(string_of_vowels), you have called the print function, and the results have been output to the STDOUT. So you do not need to use the print when call it. Because the Print function returns None (equivalent to print (None)), there will be None in the result.
You just write like this:
string_of_vowels('Casablanca')
string_of_vowels('''Casablanca is a 1942 American romantic drama film directed by Michael Curtiz and based on Murray Burnett and Joan Alison's unproduced stage play Everybody Comes to Rick's. ''')
Upvotes: 0
Reputation: 210
You are printing the result of the function even though you're already printing the vowels inside it already.
print(string_of_vowels('Casablanca'))
print(string_of_vowels('''Casablanca is a 1942 American romantic drama film directed by Michael Curtiz and based on Murray Burnett and Joan Alison's unproduced stage play Everybody Comes to Rick's. '''))
Should be:
string_of_vowels('Casablanca')
string_of_vowels('''Casablanca is a 1942 American romantic drama film directed by Michael Curtiz and based on Murray Burnett and Joan Alison's unproduced stage play Everybody Comes to Rick's. ''')
Upvotes: 1