这个while循环不会结束。有关此Python程序的任何建议吗?
[编辑:根据要求我引用了整个程序。它应该找到由两位数小数产生的最大回文。]
def palindrome(n):
first = 1
second = 1
largestPalindrome = 1
palindrome = True
while(first < 10**n):
while(second < 10**n):
number = []
candidate = 1
while candidate!=0:
number.append(candidate%10)
candidate = candidate // 10
print("in")
i = 0
ub = len(number)//2
while(i<ub):
if(number[i]!=number[len(number)-1-i]):
palindrome = False
i += 1
if palindrome == True:
largestPalindrome = first*second
print(largestPalindrome)
答案 0 :(得分:2)
您的外部while
循环
while(first < 10**n):
while(second < 10**n):
...
正在检查变量first
和second
是否在某个值(10**n
)之下。问题是,在这些循环中,你永远不会增加first
或second
,所以条件总是得到满足,你的循环会一直持续下去。