使用嵌套while循环的Hailstone序列

时间:2015-09-20 20:38:10

标签: python collatz

我正在编写一个程序,允许用户输入一系列数字,然后程序将预编码该范围内每个数字的hailstone序列,然后打印具有最大循环长度的数字。我不明白为什么我的代码不起作用。我们需要使用while循环

def main():
    #set starting variables
    start_num = int(input('Enter starting number of the range: '))
    #check if the numbers entered are positive and that the start is less than the end
    while (start_num < 1):
            start_num = int(input('Enter a positive starting number of the range: '))
    end_num = int(input('Enter ending number of the range: '))
    while (end_num < 1):
            end_num = int(input('Enter a positive ending number of the range: '))

    while (start_num > end_num):
            start_num = int(input('Enter starting number of the range: '))
            end_num = int(input('Enter ending number of the range: '))

    cycle_length = 0
    max_length = 0
    num_max = 0
    num = 0

    while (start_num < end_num):

            while (num != 1):

                    if (start_num % 2 == 0):
                            num = start_num / 2
                            cycle_length = cycle_length +1
                    else:
                            num = (start_num * 3) + 1
                            cycle_length = cycle_length +1      

                    if (cycle_length >= max_length):
                            max_length = cycle_length
                            num_max = start_num

                    cycle_length = 0
            start_num = start_num + 1

    print(num_max)
    print(max_length)

main()

2 个答案:

答案 0 :(得分:2)

while循环中,您始终会检查start_num,它永远不会发生变化。在循环的最开始,您需要将num设置为start_num。然后在整个循环体中使用num

答案 1 :(得分:0)

我只是要经历每一行并告诉你出了什么问题。你应该确保你知道每个变量持有什么以及它应该保留什么。

def main():
    #set starting variables
    start_num = int(input('Enter starting number of the range: '))
    #check if the numbers entered are positive and that the start is less than the end
    while (start_num < 1):
            start_num = int(input('Enter a positive starting number of the range: '))
    end_num = int(input('Enter ending number of the range: '))
    while (end_num < 1):
            end_num = int(input('Enter a positive ending number of the range: '))

    while (start_num > end_num):
            start_num = int(input('Enter starting number of the range: '))
            end_num = int(input('Enter ending number of the range: '))

    cycle_length = 0
    max_length = 0
    num_max = 0
    num = 0

    while (start_num < end_num):

start_numend_num都不会改变,所以你有一个无限循环,例如而(10 <100)

            while (num != 1):

num目前为0,因为您在几行之前将其设置为0后尚未将其分配给任何内容

                    if (start_num % 2 == 0):
                            num = start_num / 2

num现在是start_num/2,但start_num永远不会改变

                            cycle_length = cycle_length +1
                    else:
                            num = (start_num * 3) + 1

同样在这里

                            cycle_length = cycle_length +1      

                    if (cycle_length >= max_length):
                            max_length = cycle_length
                            num_max = start_num

您将num_max设为start_numstart_num永不改变

                    cycle_length = 0

您每个周期重置cycle_num

            start_num = start_num + 1
     print(num_max)
     print(max_length)

main()