Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36619
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: please i need explanation |
| Date | 2013-01-11 13:58 +0100 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <kcp2a1$3ke$1@r03.glglgl.gl> (permalink) |
| References | <2FB5A66FD91D456BBDFCC0AE25760392@favour> <50EFE1C3.5000303@nic.fi> <mailman.393.1357900426.2939.python-list@python.org> |
Am 11.01.2013 17:33 schrieb kwakukwatiah@gmail.com: >> def factorial(n): >> if n<2: >> return 1 >> f = 1 >> while n>= 2: >> f *= n >> f -= 1 >> return f >> >> >> > please it works. I doubt this. If you give n = 4, you run into an endless loop. > but don’t get why the return 1 and the code below. The "if n < 2: return 1" serves to shorten the calculation process below. It is redundant, as you have a "f = 1" and a "return f" for n < 2. The code below first sets f, which holds the result, to 1 and then multiplies it by n in each step. As the loop should contain a 'n -= 1', n decreases by 1 every step, turning it into f = n * (n-1) * (n-2) * ... * 2 and then, as n is not >= 2 any longer, stops the loop, returning f. HTH, Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: please i need explanation kwakukwatiah@gmail.com - 2013-01-11 10:33 -0600 Re: please i need explanation Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-11 14:20 +0200 Re: please i need explanation Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-01-11 13:58 +0100
csiph-web