ユーザーが数値の範囲を入力できるようにするプログラムを作成しています。プログラムは、範囲内の各数値の雹のシーケンスを実行し、サイクルの長さが最大の数値を出力します。ただし、コードが機能しない理由はわかりません。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()