python 判断传入的字符串参数是否为“回文联”(既可顺读,也可倒读。)

判断传入的字符串参数是否为“回文联”(既可顺读,也可倒读。)

def palindrome(words):
        words_list = list(words)
        flag_con = 1
        while True:
            if len(words_list) <= 1:
                break
            if words_list[0] == words_list[-1]:
                del words_list[0]
                words_list.pop()
            else:
                flag_con += 1
                break
    
        if flag_con == 1:
            print('输入的是回文字符')
        else:
            print('输入的不是回文字符')
    
    words = input('请输入字符串:')
    palindrome(words)

D:\Python\Python36\python.exe D:/Python_test/day_lesson/test.py
请输入字符串:aab
输入的不是回文字符

Process finished with exit code 0

D:\Python\Python36\python.exe D:/Python_test/day_lesson/test.py
请输入字符串:112211
输入的是回文字符

Process finished with exit code 0

你可能感兴趣的:(python)