无法在Python中创建阶乘函数
我的代码
import sys
number=int(sys.argv[1])
if number == 0
fact=1
else
fact=number
for (x=1; x<number; x++)
fact*=x; // mistake probably here
print fact
我收到错误
File "factorial.py", line 5
if number == 0
^
SyntaxError: invalid syntax
如何在Python中创建阶乘函数?
My code
import sys
number=int(sys.argv[1])
if number == 0
fact=1
else
fact=number
for (x=1; x<number; x++)
fact*=x; // mistake probably here
print fact
I get the error
File "factorial.py", line 5
if number == 0
^
SyntaxError: invalid syntax
How can you make a factorial function in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上,最简单的选择是:
是的,不知何故,它有效。
你怎么想不到这一点? 我不知道。
一个 for 循环和一个乘法器,真正简单是最好的方法,对吗?
编辑:哦,等等,我们正在以最高效的 cpu 效率工作? 哦哦......
really, the simplest option would be:
yes, somehow, it works.
How could you not think of this? I don't know.
A
for
loop and a multiplier, really simplicity is the best way to go, right?EDIT: Oh, wait, we're working for the most cpu-efficeint way? ohhhh.....
我的猜测,这可能是有帮助
和结果
My guess, it may be helps
and result
错误所在的行应为“
注意末尾的冒号”。
此外,您需要在 else 和 for 之后添加相同的冒号。 冒号的工作方式与其他语言中的 {} 类似。
最后,这不是 Python 中 for 循环的工作方式。 您想要使用该列表的代码将与
如果您将其放入 C 风格语言中,
您编写的代码具有相同的效果。 编辑:哎呀,我给出的 for 循环是错误的,它会包含 0。我更新了代码来纠正这个问题。
The line that your error is on should read
Note the colon on the end.
Additionally, you would need to add the same colon after the else and the for. The colons work similarly to {} in other languages.
Finally, thats not how for loops work in Python. The code you want to use that list would be
Which would have the same effect of what you wrote, if you put that in a C style language.
EDIT: Oops, the for loop I gave was wrong, it would have included 0. I updated the code to correct this.
我知道您可能出于教育原因尝试自己实现这一点。
但是,如果没有,我建议使用
math
模块内置阶乘函数(注意:需要 python 2.6 或更高版本):该模块是用 C 编写的,因此,它会非常多比用 python 编写要快。 (不过,如果您不计算大阶乘,那么无论如何它都不会太慢)。
I understand that you are probably trying to implement this yourself for educational reasons.
However, if not, I recommend using the
math
modules built-in factorial function (note: requires python 2.6 or higher):This module is written in C, and as such, it'll be much much faster than writing it in python. (although, if you aren't computing large factorials, it won't really be too slow either way).
这是您的代码,已修复并正在运行:(
阶乘零是一,对于任何不知道的人 - 我必须查找它。8-)
您需要在
if
、else 之后使用冒号
、for
等,并且for
在 Python 中的工作方式与 C 不同。Here's your code, fixed up and working:
(Factorial zero is one, for anyone who didn't know - I had to look it up. 8-)
You need colons after
if
,else
,for
, etc., and the wayfor
works in Python is different from C.Mark Rushakoff 的fact(n) 函数效率如此之高的原因是他错过了reduce() 函数。 因此它实际上从未进行过计算。
更正后的内容是(我得到了):
结果输出为 1500!,1000x:
是的,我已经检查过它们都产生相同的值!
我不明白为什么 lambda xrange 比 lambda range 差这么多。 嗯。
版本:
Win32 上的 PythonWin 2.6.2(r262:71605,2009 年 4 月 14 日,22:40:02)[MSC v.1500 32 位(Intel)]。
嗯......重新运行它我得到了一些更可信的东西
在Python 2.6.5上(r265:79063,2010年6月12日,17:07:01)[GCC 4.3.4 20090804(发布)1]在cygwin上:
全部真的是在噪音中,不是吗?
The reason Mark Rushakoff's fact(n) function was so much more efficient was that he missed-off the reduce() function. Thus it never actually did the calculation.
Corrected it reads (and I get):
Resulting output for 1500!, 1000x:
And yes, I have checked they all yield the same value!
I Can't understand why the lambda xrange is so much worse than the lambda range. Hmmm.
Version:
PythonWin 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32.
Hmm... on re-running it I get something more believable
And on Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01) [GCC 4.3.4 20090804 (release) 1] on cygwin:
All in the noise really, isn't it?
这是一个函数阶乘,您几乎要求它:
它不适用于fact(0),但您可以担心
fact
范围之外的问题:)Masi 询问函数样式是否是比 Richie 的实施更有效率。 根据我的快速基准测试(令我惊讶的是!),是的,我的速度更快。 但我们可以做一些改变。
首先,我们可以按照另一条评论中的建议用
operator.mul
替换lambda x,y: x*y
。 Python 的 lambda 运算符带来了不小的开销。 其次,我们可以用xrange
替换range
。xrange
应该在线性空间中工作,根据需要返回数字,而range
一次创建整个列表。 (然后请注意,对于过大的数字范围,您几乎肯定必须使用xrange
)因此新定义变为:
令我惊讶的是,这实际上导致性能下降。 这是 Q&D 基准:
这是我的 Python 版本,以防有人想要交叉检查我的结果:
Here's a functional factorial, which you almost asked for:
It doesn't work for fact(0), but you can worry about that outside the scope of
fact
:)Masi has asked whether the functional style is more efficient than Richie's implementation. According to my quick benchmark (and to my surprise!) yes, mine is faster. But there's a couple things we can do to change.
First, we can substitute
lambda x,y: x*y
withoperator.mul
as suggested in another comment. Python'slambda
operator comes with a not-insignificant overhead. Second, we can substitutexrange
forrange
.xrange
should work in linear space, returning numbers as necessary, whilerange
creates the whole list all at once. (Note then, that you almost certainly must usexrange
for an excessively large range of numbers)So the new definition becomes:
To my surprise, this actually resulted in slower performance. Here's the Q&D benchmarks:
Here's my Python version in case anyone wants to cross-check my results: