我假设它与我缺乏返回功能有关,但我尝试的所有内容都会带来错误,这让我觉得还有更多的事情要发生。此外,我确信这是一个更容易进行此测试的方法,我们非常感谢任何建议。提前感谢您的耐心和帮助,因为我对这一点和学习都是全新的。 print语句如下:
aaaaNone
aaaaiaAeiaoaiaaiieeiaeuiaaeouaueaoaAiououeaeaEeooeoiNone
那么,为什么"无"?
由教授给出:
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
"""
我的编码:
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
由教授给出:
# 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. '''))
答案 0 :(得分:1)
即使您已经在其中打印元音,也会打印该功能的结果。
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. '''))
应该是:
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. ''')
答案 1 :(得分:0)
在函数(string_of_vowels)中,您调用了print函数,结果已输出到STDOUT。所以你打电话时不需要使用打印。由于Print函数返回None(相当于print(None)),因此结果中将为None。
你这样写:
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. ''')
答案 2 :(得分:0)
这没有错。因为你没有给出返回值,所以会返回none。你可以修改你的调用方式,如下所示:
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. ''')
然后找不到'none',或者返回一个字符串来替换'print'。
答案 3 :(得分:0)
这个答案有点长,但需要一些时间来阅读。看起来你已经几乎已经解决了这个问题,所以我会在这里给你一个更简单的解决方案,看看Python的美妙之处,并向你展示如何在这样的问题上略微区别思考。
in
运算符您似乎已经知道in
的作用。如果x in y
位于序列x
中,y
将返回true。我正在使用&#34; sequence&#34;这里因为y
实际上可以是任何列表,元组,字符串,(或可迭代)。因此,如果要检查字符是否在字符串中,则只需使用in
即可。例如,'c' in 'acorn'
返回true,因为'acorn'
中包含'c'
。案件在这里很重要。
现在我们知道in
运算符可以使用字符串,我们可以稍微简化一下你的函数。如果您想检查x
是否为元音,可以执行:x in 'aAeEiIoOuU
,对吗?更简单。
for
循环你在答案中使用while循环,每次都有一个索引,但更简单的解决方案是迭代字符串中的每个字符,就像这样
for c in str:
if c in 'aAeEiIoOuU':
# c is a vowel here
现在,我们可以通过一起添加元音来开始跟踪元音:
vowels = ""
for c in str:
if c in 'aAeEiIoOuU':
vowels += c
此处,vowels += c
表示vowels = vowels + c
,它连接字符串。例如,"aeio" + "u"
会产生"aeiou"
。
此时,vowels
包含str
中的所有元音,现在我们需要做的就是从函数返回vowels
。返回与打印不同,在某种意义上,打印就像显示某人蛋糕的形象,而返回则给某人一个真正的蛋糕。
所以,我们的功能最终看起来像这样
def string_of_vowels(str):
vowels = ""
for c in str:
if c in 'aAeEiIoOuU':
vowels += c
return vowels