2017-04-13 13 views
0

我的問題:標識符中有訂單嗎?

我有這個練習;

If the verb ends in e, drop the e and add ing (if not exception: be, see, flee, knee, etc.) 
If the verb ends in ie, change ie to y and add ing 
For words consisting of consonant-vowel-consonant, double the final letter before adding ing 
By default just add ing 

您在本練習中的任務是定義一個函數make_ing_form(),該函數以不定式形式給出一個動詞返回其現在分詞形式。用謊言,看到,移動和擁抱等詞語測試你的功能。但是,你不能期望這樣簡單的規則適用於所有情況。

我的代碼:

def make_ing_form(): 
    a = raw_input("Please give a Verb: ") 
    if a.endswith("ie"): 
     newverb = a[:-2] + "y" + "ing" 
    elif a.endswith("e"): 
     newverb = a[:3] + "ing" 
    elif a[1] in "aeiou": 
     newverb = a + a[-1] + "ing" 
    else: 
     newverb = a + "ing" 
    print newverb 

make_ing_form() 

有了這個代碼都是腸道,但是當我改變位置;

def make_ing_form(): 
    a = raw_input("Please give a verb: ") 
    if a.endswith("e"): 
     newverb = a[:3] + "ing" 
    elif a.endswith("ie"): 
     newverb = a[:-2] + "y" + "ing" 
    elif a[1] in "aeiou": 
     newverb = a + a[-1] + "ing" 
    else: 
     newverb = a + "ing" 
    print newverb 

make_ing_form() 

答案誰我來是不是現在分詞,我這裏怎麼理解http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names,當標識符變化到另一份聲明(從如果到艾利芙),它「忘記」 if語句。如果是這樣的話,爲什麼我收到這個結果?

我的英語對不起....

+0

第二個代碼變化超過展示位置;它有一個「aeiou」:'。但是,您可以給出一個示例輸入,它提供第一個版本的正確輸出和第二個輸出的錯誤輸出? – doctorlove

+0

謝謝你的回答,第一個例子,我寫謊言,我來說謊,但第二個例子我來說謊 – d68745

+0

是的,對不起,我忘了[1]寫,我改變現在... – d68745

回答

2

在第二個代碼永遠不會進入第一ELIF(ELIF a.endswith(「IE」))。因爲如果一個動詞,即(前結束謊言)它會進入if,如同在e結尾。您應該具備第一個代碼中的條件。如果你的第一個代碼有更多問題,請告訴我。

+0

謝謝你的答案,但是我在第二個代碼中遇到的問題,第一個代碼就是這一切直覺....你認爲第二個代碼的問題是來自條件? – d68745

+0

有時候我只專注於代碼方面,而不是在細節方面或編程方面都很重要...... – d68745

+0

是的,我認爲條件的順序是什麼影響你的代碼。有沒有你不使用第一個代碼的原因? – Melannie