简体   繁体   English

Python嵌套循环-无输出

[英]Python nested loops - no output

I've just started learning python. 我刚刚开始学习python。 I am trying to check if the integer x is palindrome then divide it by a number between range (starting from largest y ie 999 ) y=999,998,...,100. 我试图检查整数x是否是回文,然后将其除以范围之间的数字(从最大的y即999开始)y = 999,998,...,100。 If x/y=z and z is also a 3 digit integer then finish. 如果x / y = z并且z也是3位整数,则结束。 Otherwise subtract 1 from x and do the same procedure. 否则,从x减去1并执行相同的步骤。

def EuQ4():
    x=998001
    p=999
    while 10000 < x:
        x=x-1
        if str(x)== str(x)[::-1]:
            while p>100:
                if x%p==0:
                    Pal=x/p
                    if Pal < 999:
                        print (Pal,p)
                        break
                    else:
                        x=x-1
                else:
                    p=p-1
        else:
            x=x-1
EuQ4()

This is question 4 from Project Euler ie Find the largest palindrome made from the product of two 3-digit numbers. 这是来自Euler项目的问题4,即找到由两个3位数字的乘积构成的最大回文。

You have a few logic errors in here. 您这里有一些逻辑错误。 Some cause loops that just never end. 有些原因循环永远不会结束。 For example, what happens when x % p == 0 but Pal is larger 999 ? 例如,当x % p == 0Pal大于999时会发生什么? You would get an infinite loop. 您将得到一个无限循环。

I made a few modifications, but it could still use some work. 我做了一些修改,但仍然可以使用。

def EuQ4():
    x = 998001
    while 10000 < x:
        if str(x) == str(x)[::-1]:
            print("{} is a pali!".format(x))
            # Move it here so each time it stats at the right
            # number or else it will just skip it after it does it once.
            p = 999
            while p > 100:
                if x % p == 0:
                    pali = int(x / p)
                    if pali < 999:
                        print(pali, p)
                        return
                p -= 1
        x -= 1

EuQ4()

Edit: 编辑:

I found these mistakes by using the debugger in my IDE. 我通过在IDE中使用调试器发现了这些错误。 You could have easily done the same thing by going through the code line by line a few times. 您可以通过逐行浏览几次代码来轻松完成相同的操作。

I am sorry but it was hurting my head to read your question. 抱歉,阅读您的问题很伤脑筋。 If you are trying to learn Python while attempting these questions then I would propose this alternate answer - it does not answer your question but it does lead to the solution and I think it is more Pythonic. 如果您在尝试这些问题时尝试学习Python,那么我将建议您提供替代答案-它不能回答您的问题,但确实可以解决问题,并且我认为它更像Python。 The question asks to find the largest palindrone made from the product of two 3 digit numbers. 该问题要求找到由两个3位数字的乘积制成的最大的palindrone。 So the inputs should be 3 digit numbers. 因此,输入应为3位数字。 This code will allow you to specify the number of digits, max and min (as integers). 该代码将允许您指定最大和最小位数(以整数形式)。

I am not proposing that this be the best solution the the Euler Problem posed rather it is a solution that gives you exposure to a range of features in Python. 我并不是在提议欧拉问题提出的最佳解决方案,而是要让您了解Python的一系列功能。

def min_value(integer):
    min_val = '1'
    for n in range(0,integer-1):
       min_val+='0'
    return int(min_val)

def max_value(integer):
    max_val = '9'
    for n in range(0,integer-1):
       max_val += '9'
   return int(max_val) +1

def find_max_palindrones(x,y):
    minimum_value = min_value(x)
    maximum_value = max_value(y)
    palindrones = []
    working_range = [number for number in range(minimum_value,maximum_value,1)]
    for x_value in working_range:
        for y_value in working_range:
            product = x_value * y_value
            orig_order = [item for item in str(product)]
            rev_order = [item for item in str(product)[::-1]]
            if orig_order == rev_order:
                palindrones.append(product)
    max_p = max(palindrones)
    return max_p

>>>find_max_palindrones(3,3)
906609

Put p=999 befor while p > 100 or use for p in range(999, 100, -1) . while p > 100之前将p=999放进去while p > 100或者将for p in range(999, 100, -1)

p = 999
while p > 100

And I think you call x=x-1 too many times. 而且我认为您多次致电x=x-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM