2017-04-25 19 views
0

我有一個問題,我不得不使用一個函數來輸出單詞中的元音並輸出有多少元音。我剛剛在當地大學學習了一門計算機課程,對這件事情是新鮮的,它有點過頭了。在單詞中查找元音的函數?

我試過,但是這是我得到的時候我跑了它:

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) 
+1

請將您的代碼包裝在代碼標籤中。 – elena

+0

看,你的函數定義不接受任何參數,而在「元音(單詞)」中,你確實發送了一個參數。 – elena

+0

只需改變'def vowel():'到'def vowel(word):',你的代碼就可以正常工作。 – davedwards

回答

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

+0

簡單的錯誤:)我現在感覺像一個白癡,但謝謝你這麼多 – MasterBaggins2319

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) 
+0

感謝程序現在運行良好 – MasterBaggins2319

1

其實,錯誤的是簡單的。

當您定義函數元音時,它不會收到任何參數。

它應該是這樣的:

def vowel (word): 

希望我可以幫助你:d

+0

非常感謝你你真的幫我了:) – MasterBaggins2319

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) 
+0

非常感謝你的程序很好的工作 – MasterBaggins2319

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) 
+0

非常感謝它現在的作品現在 – MasterBaggins2319