/ Sequência / Hailstone usando loops aninhados while - python, collatz

Sequência de granizo usando loops aninhados ao mesmo tempo - python, collatz

Eu estou escrevendo um programa que permite ao usuárioinsira um intervalo de números e, em seguida, o programa executará uma sequência de granizo de cada número no intervalo e, em seguida, imprimirá o número com o maior comprimento do ciclo. Eu não posso ver porque meu código não está funcionando embora. Somos obrigados a usar loops 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()

Respostas:

2 para resposta № 1

Na tua while loop, você está sempre verificando start_num, que nunca muda. No início do ciclo, você precisa definir num para start_num. Então trabalhe com num em todo o corpo do loop.


0 para resposta № 2

Eu vou passar por cada linha e dizer o que está errado. Você deve realmente certificar-se de que você sabe o que cada variável está segurando e o que ela deve manter.

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):

ambos start_num e end_num nunca muda, então você tem um loop infinito, por exemplo enquanto (10 <100)

            while (num != 1):

num está atualmente em 0, já que você não o atribuiu a nada depois de defini-lo como 0 algumas linhas atrás

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

num é agora start_num/2, mas start_num nunca muda

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

o mesmo aqui

                            cycle_length = cycle_length +1

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

você está se pondo num_max para start_num mas start_num nunca muda

                    cycle_length = 0

você está redefinindo cycle_num todo ciclo

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

main()