Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.04; 'syntax': 0.04; 'odd': 0.07; 'remaining': 0.07; 'fix.': 0.09; 'follows.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spaces': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; '(1,': 0.16; '(2,': 0.16; '(3,': 0.16; 'editor,': 0.16; 'idle,': 0.16; 'iteration': 0.16; 'out)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'skip:[ 40': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'obviously': 0.18; 'seems': 0.21; 'input': 0.22; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'tells': 0.24; '(or': 0.24; 'question': 0.24; 'second': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; '[1]': 0.29; 'code': 0.31; 'lines': 0.31; '"the': 0.34; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'should': 0.36; 'error.': 0.37; 'example,': 0.37; 'positive': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'space': 0.40; 'even': 0.60; 'above,': 0.60; 'received:173': 0.61; 'back': 0.62; 'email addr:gmail.com': 0.63; 'repeat': 0.74; 'algorithm,': 0.84; 'received:fios.verizon.net': 0.84; 'subject:Friend': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Newbie - Trying to Help a Friend Date: Thu, 21 Nov 2013 19:17:10 -0500 References: <0e127888-4bfa-4f14-aa55-df8ef53284a3@googlegroups.com> <528c31e9$0$11089$c3e8da3@news.astraweb.com> <8e4ju.71474$Xe4.3764@fx34.am4> <15a6e59c-1ebc-44dc-9441-b2410a49ab98@googlegroups.com> <528DF430.1060608@rece.vub.ac.be> <29330860-484d-4cc8-b01c-0460fb55c251@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <29330860-484d-4cc8-b01c-0460fb55c251@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385079448 news.xs4all.nl 15877 [2001:888:2000:d::a6]:56385 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60184 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 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