I'm stuck trying to figure out how to find out which number in a range entered for a hailstone sequence has the longest length of numbers total including the starting number itself. This is what I have so far:

elif choice is apart of a menu

getValidInt() is a function that makes sure the value entered.

hailstone() is where i have my hailstone loop.

I know I need some type of if statement that saves the new number that has the longest chain, then once I know the number I just print out the length of the longest sequence.

elif choice == 'L' or choice == 'l':
            startNum = getValidInt('Enter the first number in the range', 1, 10000)
            stopNum = getValidInt('Enter the first number in the range', startNum, 10000)

            longLength = 0
            for i in range(startNum, stopNum + 1):
                longLength = hailstone(i)
                if i >= longLength:
                    longLength = i
            print longLength

any help would be great I'm stumped. Thanks.

longLength = 0
            for i in range(startNum, stopNum + 1):
                longLength = hailstone(i)
                if i >= longLength:
                    longLength = i
            print longLength

You are using "i", the incremented value in the for loop and copying it to longLength. Since we don't know what hailstone is, I am assuming it is a list. When you get some time, please read the coding conventions/style guide for Python http://www.python.org/dev/peps/pep-0008/

long_length = -1
length_num = -1
for n in range(startNum, stopNum + 1):
    if hailstone[n] > long_length:
       long_length = hailstone[n]
       length_num = n
print long_length, length_num
#
# also you can use
elif choice.upper() == 'L':
# for
elif choice == 'L' or choice == 'l':

Edited 7 Years Ago by woooee: n/a

You are using "i", the incremented value in the for loop and copying it to longLength.

long_length = -1
length_num = -1
for n in range(startNum, stopNum + 1):
    if hailstone[n] > long_length:
       long_length = hailstone[n]
       length_num = n
print long_length, length_num

what does the [n] in mean?

Also, this does not work, It just prints the sequence twice for each number.

Edited 7 Years Ago by scrace89: n/a

This article has been dead for over six months. Start a new discussion instead.