我的程序有效,但Codeacademy不承认

时间:2013-09-01 20:05:44

标签: python

这是我的猪拉丁语翻译代码。它适用于Code academy和linux终端。

pyg = 'ay'
new_word = pyg

original = raw_input('Enter a word: ')

if len(original) > 0 and original.isalpha():
    original.lower()
    word = original
    first = original[0]
    if first  == 'a' or first =='e' or first == 'i' or first =='o' or first == 'u':
        print 'vowel'

    elif first != 'a' or first !='e' or first !='o' or first !='i' or first !='u':


        print  word.lower()[1:] + first +new_word 

else:
    print 'empty'

代码学院给出以下结果; 哎呀,再试一次!你的单词以辅音开头,但是“ay”被打印而不是“ogday”。确保将正确的值#存储在“new_word”中。

“ay”未打印,但打印“ogday”。

有谁知道如何解决这个问题?如果不解决这个问题,我无法继续使用Codeacademy。

2 个答案:

答案 0 :(得分:0)

例如,您可以执行此类操作。您正走在正确的轨道上,只需使用您在代码学院学到的知识即可完成此任务。

consonants = "bcdfghjklmnpqrstvxz"
original = raw_input('Enter a word: ')

if len(original) > 0 and original.isalpha():
   if original.lower()[0] in 'aeiou':
      print original.lower() + 'ay'
   else:
      keep_first_consonants = ''
      count = 0
      for letter in original.lower():
          if letter in consonants:
             keep_first_consonants = keep_first_consonants + letter
             count += 1
          else:
             break
      total_characters = len(original.lower())
      print original.lower()[count:total_characters] + keep_first_consonants + 'ay'
else:
    print 'Accept only letters'

答案 1 :(得分:0)

codeacademy课程检查器似乎在您点击运行时检查变量new_word

所以你只需要为你的打印变量使用new_word

此代码有效:

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
        new_word = original + pyg
        print new_word
    else:
        newer_word = word[1:]
        new_word = newer_word + first + pyg
        print new_word