无法在Python中进行阶乘函数 [英] Unable to make a factorial function in Python

查看:95
本文介绍了无法在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中创建阶乘函数?

推荐答案

这是您的代码,已修复并正常工作:

Here's your code, fixed up and working:

import sys
number = int(sys.argv[1])
fact = 1
for x in range(1, number+1):
    fact *= x

print fact

(对于任何一个不知道的人,零零都是一个-我必须查一下.8-)

(Factorial zero is one, for anyone who didn't know - I had to look it up. 8-)

ifelsefor等之后需要冒号,并且for在Python中的工作方式不同于C.

You need colons after if, else, for, etc., and the way for works in Python is different from C.

这篇关于无法在Python中进行阶乘函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆