/ / Hailstone sekwencji przy użyciu zagnieżdżonych podczas pętli - python, collatz

Hailstone sekwencja za pomocą zagnieżdżonych podczas pętli - python, collatz

Piszę program, który pozwala użytkownikowiwprowadź zakres liczb, a następnie program wykona sekwencję gradową każdej liczby w zakresie, a następnie wydrukuje liczbę o największej długości cyklu. Nie rozumiem, dlaczego mój kod nie działa, ale musimy używać pętli 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()

Odpowiedzi:

2 dla odpowiedzi № 1

W Twoim while pętli, zawsze sprawdzasz start_num, który nigdy się nie zmienia. Na samym początku pętli musisz ustawić num do start_num. Następnie pracuj z num w całym ciele pętli.


0 dla odpowiedzi nr 2

Po prostu przejdę przez każdą linijkę i powiem ci, co jest nie tak. Powinieneś naprawdę upewnić się, że wiesz, co każda zmienna trzyma i co ma trzymać.

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

obie start_num i end_num nigdy się nie zmienia, więc masz nieskończoną pętlę, np. natomiast (10 <100)

            while (num != 1):

num jest obecnie 0, ponieważ nie przypisałeś go do niczego po ustawieniu go na 0 kilka linii temu

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

num jest teraz start_num/2, ale start_num nigdy się nie zmienia

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

to samo tutaj

                            cycle_length = cycle_length +1

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

jesteś ustawienie num_max do start_num ale start_num nigdy się nie zmienia

                    cycle_length = 0

resetujesz cycle_num każdy cykl

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

main()