我遇到一个问题,我必须使用一个函数来输出单词中的元音并输出有多少个元音。我刚刚在当地的大学学习了一门计算机课程,对这一切都很陌生,而且它有点过了我的脑袋。
我试过但这是我跑的时候得到的:
TypeError: vowel() takes 0 positional arguements but 1 was given
我的代码:
def vowel ():
array = []
counter = 0
for i in word:
if i in ("a","e","i","o","u"):
counter+=1
array.append(i)
return (array, counter)
word = input("Enter your word")
function = vowel(word)
print(function)
答案 0 :(得分:1)
实际上,错误很简单。
当您定义函数 Vowel 时,它不会收到任何参数。
它应该是这样的:
def vowel (word):
希望我能帮到你:D
答案 1 :(得分:1)
函数定义和函数调用中给出的参数数量应相同。在函数定义中,您编写了def vowel():
,但在调用function = vowel(word)
时,您正在为函数提供参数。因此它会抛出错误。您可以将其修改为:
def vowel(word):
array = []
counter = 0
for i in word:
if i in ("a","e","i","o","u"):
counter+=1
array.append(i)
return (array, counter)
word = input("Enter your word")
function = vowel(word)
print(function)
答案 2 :(得分:0)
在功能定义中,您忘记提供word
def vowel (word):
array = []
counter = 0
for i in word:
if i in ("a","e","i","o","u"):
counter+=1
array.append(i)
return (array, counter)
word = input("Enter your word")
function = vowel(word)
print(function)
要了解有关位置参数的更多信息,请参阅此帖子Positional argument v.s. keyword argument
答案 3 :(得分:0)
错误是不言自明的。 你声明函数元音是用0参数播种的。
def vowel ():
你打电话给它用一个参数播种:
function = vowel(word)
你应该做的是:
def vowel (word):
array = []
counter = 0
for i in word:
if i in ("a","e","i","o","u"):
counter+=1
array.append(i)
return (array, counter)
答案 4 :(得分:0)
你可以使用一个函数,然后使用简单的列表理解:
def vowel(word):
array = [i for i in word if i in ("a","e","i","o","u")]
counter = len(array)
return array, counter
word = input("Enter your word")
function = vowel(word)
print(function)