Newbie - Trying to Help a Friend

Gary Herron gary.herron at
Fri Nov 22 00:35:41 CET 2013

On 11/21/2013 03:17 PM, bradleybooth12345 at wrote:
> Coming back to the second question
> "The collatz process is as follows. Take a positive integer n greater than 1. while n is greater than 1 repeat the following; if N is even halve it and if N is odd multiply it by 3 and add 1. The (Unsolved) collatz conjecture is that this process always terminates.
> The user should be prompted to supply the number n, and your program should build the list of values taken by sucessive iteration of the algorithm, and print it out. For example, if 7 is input your program should print the list
> [7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
> We've managed to come up with this, but obviously it's wrong. Any Idea's?
> def collatz_sequence (n) :
>        seq = [ ]
>        if n < 1 :
>           return [ ]
>        while n > 1:
>             if n % 2 == 0:
>              n = n/2
>            else:
>               n = 3*n+ 1
>            seq.append (n)
>       return seq

Why  do you say it's wrong?   What does it do?   What was expected?

I see that your indentations don't match, but I can't tell if that's 
your error or an email problem.  Is that the 'obviously wrong' part?

I also see that you create an (apparently correct) function, which 
returns a nice result.  But you haven't called the function to actually 
run it with a specific value to be printed out.  Perhaps that's the  
'obviously wrong' part you refer to.

However, the function itself looks correct otherwise, although you may 
want to start the sequence off with [n] rather than [] so as to match 
the suggested output.

Gary Herron

More information about the Python-list mailing list