Python程序输出不正确?

时间:2013-09-25 00:31:07

标签: python python-3.x

我最近开始学习如何使用站点Codecademy.com在Python中编程,它使用Python 2.7,虽然我已经在我的计算机上安装了2.7.3和3.3.2,但我在Python 3中创建程序

该程序本身是一个简单的概念证明,来自该网站的课程,猪拉丁语翻译。我已经决定更进一步,开发它以处理整个文本段落,包括空格和其他这样的实例,而不是只有一个单词,这是程序最初做的。

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

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

以下是代码:

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 个答案:

答案 0 :(得分: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'):

其中将始终为True,因为非空字符串被视为True。

将其更改为:

if first_letter in 'aeiou':


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

答案 1 :(得分:0)

最后的其他块缺少匹配if。省略最后的“其他:”,它可能会起作用。