Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60184

Re: Newbie - Trying to Help a Friend

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Newbie - Trying to Help a Friend
Date 2013-11-21 19:17 -0500
References (15 earlier) <528DF430.1060608@rece.vub.ac.be> <CAPTjJmpE1qogG59KqMvO_kO3fxRxPc5EvvyazLEeFTZm4smqEQ@mail.gmail.com> <CAHWX4tH-uFV3YXGPFPX9hRihHT2+WFOXM-gan4zp3aFCzMQqXg@mail.gmail.com> <mailman.3012.1385053851.18130.python-list@python.org> <29330860-484d-4cc8-b01c-0460fb55c251@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3020.1385079448.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 11/21/2013 6:17 PM, bradleybooth12345@gmail.com 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]

The specification does not say what the result should be when the input 
is 1, but given the example above, [1] seems reasonable (rather than an 
exception or []). [] is ok for 0 (or negative).

> We've managed to come up with this, but obviously it's wrong.

In what way is it wrong? The answer to that tells you what to fix.
A syntax error about indentation? Fix the indentation.

 >  Any Idea's?

Write test code before writing the function.

for inn,out in [(0, []), (1, [1]), (2, [2,1]),
         (3, [3,10,5,16,8,4,2,1]),
         (7, [7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]), ]:
     s = collatz(inn)
     print(inn, ':', s)
     if s != out:
         print('is not', out)

> def collatz_sequence (n) :
>        seq = [ ]

4 space indents are best; a good editor, like the one with Idle, will 
convert <tab> to 4 spaces

>        if n < 1 :
>           return [ ]
>        while n > 1:
>             if n % 2 == 0:

dedent if so it lines up with else below

>              n = n/2
>            else:
>               n = 3*n+ 1
>            seq.append (n)
>       return seq

does not line up with while

Once you get indents consistent, test failure should suggest the 
remaining error.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie - Trying to Help a Friend bradleybooth12345@gmail.com - 2013-11-19 10:40 -0800
  Re: Newbie - Trying to Help a Friend maxwell34m@gmail.com - 2013-11-19 11:05 -0800
  Re: Newbie - Trying to Help a Friend Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-19 14:07 -0500
  Re: Newbie - Trying to Help a Friend Gary Herron <gary.herron@islandtraining.com> - 2013-11-19 11:06 -0800
  Re: Newbie - Trying to Help a Friend bradleybooth12345@gmail.com - 2013-11-19 11:27 -0800
    Re: Newbie - Trying to Help a Friend Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-19 20:48 +0000
      Re: Newbie - Trying to Help a Friend Duncan Booth <duncan.booth@invalid.invalid> - 2013-11-20 11:38 +0000
        Re: Newbie - Trying to Help a Friend Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-23 02:03 +0000
    Re: Newbie - Trying to Help a Friend Mark Janssen <dreamingforward@gmail.com> - 2013-11-19 14:51 -0800
  Re: Newbie - Trying to Help a Friend Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-20 00:17 +0000
    Re: Newbie - Trying to Help a Friend Dave Angel <davea@davea.name> - 2013-11-19 22:10 -0500
      Re: Newbie - Trying to Help a Friend Steven D'Aprano <steve@pearwood.info> - 2013-11-20 03:52 +0000
        Re: Newbie - Trying to Help a Friend Dave Angel <davea@davea.name> - 2013-11-20 00:54 -0500
          Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 09:29 +0000
            Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 13:57 +0000
              Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 14:51 +0000
                Re: Newbie - Trying to Help a Friend Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-20 18:33 -0500
                Re: Newbie - Trying to Help a Friend Anssi Saari <as@sci.fi> - 2013-11-21 15:44 +0200
              Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 14:49 +0000
                Re: Newbie - Trying to Help a Friend Chris Angelico <rosuav@gmail.com> - 2013-11-21 02:05 +1100
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:06 +0000
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:09 +0000
                Re: Newbie - Trying to Help a Friend Chris Angelico <rosuav@gmail.com> - 2013-11-21 02:14 +1100
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:35 +0000
                Multiple postings Alister <alister.ware@ntlworld.com> - 2013-11-20 15:36 +0000
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:34 +0000
                Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 16:29 +0000
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 16:45 +0000
                Re: Newbie - Trying to Help a Friend Ned Batchelder <ned@nedbatchelder.com> - 2013-11-20 09:12 -0800
                Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 17:37 +0000
                Re: Newbie - Trying to Help a Friend Ned Batchelder <ned@nedbatchelder.com> - 2013-11-20 09:51 -0800
                Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 18:09 +0000
                Re: Newbie - Trying to Help a Friend Ned Batchelder <ned@nedbatchelder.com> - 2013-11-20 10:18 -0800
                Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 18:35 +0000
                Re: Newbie - Trying to Help a Friend Ned Batchelder <ned@nedbatchelder.com> - 2013-11-20 11:54 -0800
                Re: Newbie - Trying to Help a Friend Chris Angelico <rosuav@gmail.com> - 2013-11-21 07:26 +1100
                Off-topic: Pop culture references [was Re: Newbie - Trying to Help a Friend] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-21 00:27 +0000
                Re: Off-topic: Pop culture references [was Re: Newbie - Trying to Help a Friend] MRAB <python@mrabarnett.plus.com> - 2013-11-21 00:48 +0000
                Re: Off-topic: Pop culture references [was Re: Newbie - Trying to Help a Friend] Tim Golden <mail@timgolden.me.uk> - 2013-11-21 09:08 +0000
                Re: Off-topic: Pop culture references [was Re: Newbie - Trying to   Help a Friend] Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-22 17:10 +1300
                Re: Off-topic: Pop culture references [was Re: Newbie - Trying to Help a Friend] Chris Angelico <rosuav@gmail.com> - 2013-11-21 20:46 +1100
                Re: Newbie - Trying to Help a Friend Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-21 12:53 +0100
                Re: Newbie - Trying to Help a Friend Chris Angelico <rosuav@gmail.com> - 2013-11-22 02:48 +1100
                Re: Newbie - Trying to Help a Friend Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-21 11:29 -0500
                Re: Newbie - Trying to Help a Friend Chris Angelico <rosuav@gmail.com> - 2013-11-22 04:10 +1100
                Re: Newbie - Trying to Help a Friend bradleybooth12345@gmail.com - 2013-11-21 15:17 -0800
                Re: Newbie - Trying to Help a Friend Gary Herron <gary.herron@islandtraining.com> - 2013-11-21 15:35 -0800
                Re: Newbie - Trying to Help a Friend bradleybooth12345@gmail.com - 2013-11-21 15:55 -0800
                Re: Newbie - Trying to Help a Friend Terry Reedy <tjreedy@udel.edu> - 2013-11-21 19:55 -0500
                Re: Newbie - Trying to Help a Friend Gary Herron <gary.herron@islandtraining.com> - 2013-11-21 20:17 -0800
                Re: Newbie - Trying to Help a Friend Terry Reedy <tjreedy@udel.edu> - 2013-11-21 19:17 -0500
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:34 +0000
                Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 15:06 +0000
                Re: Newbie - Trying to Help a Friend Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 15:24 +0000
              Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 14:50 +0000
              Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 14:50 +0000
          Re: Newbie - Trying to Help a Friend Alister <alister.ware@ntlworld.com> - 2013-11-20 09:29 +0000

csiph-web