-1

I'm writing a program that allows the user to input a range of numbers, and then the program will preform a hailstone sequence of each number in the range and will then print the number with the largest cycle length. I cant see why my code isn't working though. We are required to use while loops

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()
Rsherrill
  • 129
  • 3
  • 4
  • 12

2 Answers2

2

In your while loop, you're always checking start_num, which never changes. At the very beginning of the loop, you need to set num to start_num. Then work with num throughout the loop body.

saulspatz
  • 5,011
  • 5
  • 36
  • 47
0

I'm just going to go through each line and tell you what's wrong. You should really make sure that you know what each variable is holding and what it is supposed to hold.

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

both start_num and end_num never change, so you have an infinite loop, e.g. while(10 < 100)

            while (num != 1):

num is currently 0, as you have not assigned it to anything after you set it to 0 a few lines ago

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

num is now start_num/2, but start_num never changes

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

same here

                            cycle_length = cycle_length +1      

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

you are setting num_max to start_num but start_num never changes

                    cycle_length = 0

you are resetting cycle_num every cycle

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

main()
Azsgy
  • 3,139
  • 2
  • 29
  • 40