我的问题:
我有这个练习;
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到Elif)时,它会忘记" if语句。如果是这样,为什么我会收到这个结果?
抱歉我的英语......答案 0 :(得分:2)
在第二个代码中,它永远不会进入第一个elif(elif a.endswith(“ie”):)因为如果一个动词以ie结尾(例如谎言),它将输入if,因为谎言以e结尾。您应该具有第一个代码中的条件。如果你的第一个代码有更多问题,请告诉我。