循环判断是否为回文(Python实现)

    本程序在Python3的IDLE环境下运行通过。
def isPalindrome():
    i = int(input('Enter a positive number:'))
    while i <= 0:
        i=int(input('Enter a positive number:'))
    i = str(i)
    n = len(i)
    start,end = 0,n-1
    tag =True #默认是回文。
    while start <= end:
        if i[start] != i[end]:
            tag = False
            break
        start = start+1
        end = end -1
    return tag

def main():
    if isPalindrome():
        print('This is the palindrome')
    else:
        print('This isn\'t the palindrome')
if __name__=='__main__':
    main()
     值得一提的是,在Python中,变量是以内容为基准,不是像C语言那样以变量名为基准。Python不使用自增、自减的原因是—编译解析上的简洁和语言本身的简洁。Python中的一个变量可以被多个名称访问,这就导致了数字类型的值是不能去改变的。

猜你喜欢

转载自blog.csdn.net/qq_41822235/article/details/80200712