我在Python中为Project Euler编写了以下代码:Largest palindrome product
y = 1000
x = 1000
while y >= 100:
y-=1
while x >= 100:
x-=1
product = y*x
string = str(product)
if str(product) == string[::-1]:
print(product)
但这不会在闲置中输出任何内容......问题是什么?
答案 0 :(得分:2)
您需要在外部循环中重新初始化x
:
while y >= 100:
x = 1000
...
答案 1 :(得分:2)
您需要在循环中初始化x。但它更容易用于循环,并完全避免初始化问题。
for y in range(100, 1000):
for x in range(100, 1000):
prod = x * y
if str(prod) == str(prod)[::-1]:
print(prod)
答案 2 :(得分:0)
##对于初学者...这可能对您有帮助! ##
pro=[]
fin=[]
my_num=[]
x=[]
rev_num=[]
for a in range (100,1000):
for b in range (100,1000):
my_num.append(str(a*b))
l1= len(my_num)
for i in range(0,l1):
rev_num.append(my_num[i][::-1])
for j in range(0,l1):
if(my_num[j]==rev_num[j]):
pro.append(int(my_num[j]))
pro.sort()
print(f'Largest palindrome product in the range is {pro[-1]}')