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.

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.