2013-09-25 59 views
0

我最近开始学习如何使用Codecademy.com在Python中进行编程,它使用Python 2.7,虽然我在计算机上安装了2.7.3和3.3.2,但我正在使用Python 3创建程序。Python程序输出不正确?

该程序本身是一个简单的概念证明,从网站上的教训,猪拉丁语翻译。我决定更进一步,将其发展到处理整个段落的文本,包括空间和其他这样的实例,而不是只有一个单词,这是该程序最初做的。

我现在的问题是,无论我接受什么,程序只输出相同的东西,我不知道为什么。

它只输出'这还没完成'。代码的打印,这是多个单词的实例,显然还没有完成。

下面的代码:

pyg = 'ay' 

raw_input = input('Enter your text here. Numbers are not allowed. ') 

if len(raw_input) > 0 and raw_input.replace(' ', '').isalpha: 
lower_input = raw_input.lower() 

if lower_input[0] == " ": 
    lower_input = lower_input[1:] 

word_spacing = lower_input.replace(' ', '/') 

if word_spacing.find('/'): 
    print('This isn\'t finished yet.') 

else: 
    first_letter = raw_input[0] 

    if first_letter == 'a' or 'e' or 'i' or 'o' or 'u': 
     output = raw_input[1].upper() + raw_input[2:] + first_letter + pyg 
     print(output) 

    else: 
     output = raw_input[0].upper() + raw_input[1:] + pyg 
     print(output) 

else: 
print('The text you entered is invalid.') 

end = input('Press Enter to exit') 

如果有人能读懂了这些代码并帮我调试这一点,那将是真正的帮助。一直盯着它,但仍然没有得到它。

回答

2

raw_input.replace(' ', '').isalpha

你没有调用函数isalpha,只引用它。添加()


if first_letter == 'a' or 'e' or 'i' or 'o' or 'u':

是一样的:

if (first_letter == 'a') or ('e') or ('i') or ('o') or ('u'):

这将永远是真实的,因为非空字符串被认为是真实的。

将其更改为:

if first_letter in 'aeiou':


你也忘了在底部缩进print('The text you entered is invalid.')

+0

呃,格式化在这里很奇怪,必须习惯它。在我的原始代码中,缩进全部都是正确的,我会尽量在稍后提交时更好地检查它。谢谢您的帮助。 – Duskite

+0

@Duskite没问题:)。不要忘记[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) – TerryA

0

最后的else块没有匹配if。省略最后的“else:”并且可能工作。