在python中打印两个条件输出

时间:2015-02-11 22:49:20

标签: python-3.x

下面是一个小程序,如果单词是plaindrome的正向流程工作正常并给出正确的输出,但如果单词不是回文,则它打印两个打印语句。任何帮助赞赏..

x=input("Enter the word or sentance :")
a=len(x)
if a==1:
    print("it is a one letter palindrome")
elif a>1:
    y=0
    while y<(a/2):
        if x[y]==x[-(y+1)]:
            print(x[y])
        else:
            print("it is not palindrome") 
            break
        y=y+1
       print("it is a palindrome")

1 个答案:

答案 0 :(得分:0)

您在最后一次打印调用之前缺少一个空格,因此在while循环终止后调用它。

更新

Python对间距很敏感,我无法看到你的评论中是否修复了这个问题。试试这个:

x=input("Enter the word or sentance :")
a=len(x)
if a==1:
    print("it is a one letter palindrome")
elif a>1:
    y=0
    while y<(a/2):
        if x[y]==x[-(y+1)]:
            print(x[y])
        else:
            print("it is not palindrome") 
            break
        y=y+1
        print("it is a palindrome")
相关问题