Python 使用嵌套while循环的冰雹序列

Python 使用嵌套while循环的冰雹序列,python,collatz,Python,Collatz,我正在编写一个程序,允许用户输入一系列数字,然后该程序将对该范围内的每个数字执行冰雹序列,然后打印周期长度最大的数字。我不明白为什么我的代码不起作用。我们需要使用while循环 def main(): #set starting variables start_num = int(input('Enter starting number of the range: ')) #check if the numbers entered are positive and tha

我正在编写一个程序,允许用户输入一系列数字,然后该程序将对该范围内的每个数字执行冰雹序列,然后打印周期长度最大的数字。我不明白为什么我的代码不起作用。我们需要使用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()
def main():
#设置起始变量
start_num=int(输入('输入范围的起始编号:'))
#检查输入的数字是否为正数,起点是否小于终点
而(开始数量<1):
start_num=int(输入('输入范围的正起始数字:'))
end_num=int(输入('输入范围的结束编号:'))
而(结束数量<1):
end_num=int(输入('输入范围的正结束数:'))
而(开始数量>结束数量):
start_num=int(输入('输入范围的起始编号:'))
end_num=int(输入('输入范围的结束编号:'))
循环长度=0
最大长度=0
num_max=0
num=0
而(开始数量<结束数量):
而(num!=1):
如果(开始数量%2==0):
num=start\u num/2
循环长度=循环长度+1
其他:
数值=(开始数值*3)+1
循环长度=循环长度+1
如果(循环长度>=最大长度):
最大长度=循环长度
num\u max=开始\u num
循环长度=0
开始数量=开始数量+1
打印(最大数量)
打印(最大长度)
main()

在您的
循环中,您总是在检查
开始数,该值从不更改。在循环的最开始,您需要将
num
设置为
start\u num
。然后在整个循环体中使用
num

我将遍历每一行,并告诉您出了什么问题。您应该确保您知道每个变量所包含的内容以及它应该包含的内容

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):
num
当前为0,因为在几行之前将其设置为0后,您尚未将其分配给任何对象

                    if (start_num % 2 == 0):
                            num = start_num / 2
num
现在是
start\u num/2
,但
start\u num
从不更改

                            cycle_length = cycle_length +1
                    else:
                            num = (start_num * 3) + 1
                    cycle_length = 0
这里也一样

                            cycle_length = cycle_length +1      

                    if (cycle_length >= max_length):
                            max_length = cycle_length
                            num_max = start_num
您正在将
num\u max
设置为
start\u num
,但
start\u num
从未更改

                            cycle_length = cycle_length +1
                    else:
                            num = (start_num * 3) + 1
                    cycle_length = 0
您正在每个周期重置
cycle\u num

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

main()

即使这样,程序也不会打印任何东西。它只是无限期地运行。start_num=start_num+1是否不会在每次循环时更改其值?
start_num
仅更改为其生成序列的每个数字的值