Python 2.7 一个用户在python中输入三个不同的输出,代码不起作用

Python 2.7 一个用户在python中输入三个不同的输出,代码不起作用,python-2.7,fibonacci,factorial,Python 2.7,Fibonacci,Factorial,在我的家庭作业中,用户应该输入一个数字并显示阶乘、斐波那契级数和所有立方数字,直到用户在Python中输入的数字为止,但用户无法找出问题出在哪里 #!/Python27/python def factorial( n ): if n <1: # base case return 1 else: return n * factorial( n - 1 ) # recursive call def fact(n): for i in range(1, n+1

在我的家庭作业中,用户应该输入一个数字并显示阶乘、斐波那契级数和所有立方数字,直到用户在Python中输入的数字为止,但用户无法找出问题出在哪里

#!/Python27/python
def factorial( n ):
  if n <1:   # base case
    return 1
  else:
   return n * factorial( n - 1 )  

# recursive call
def fact(n):
  for i in range(1, n+1 ):
    print "%d" % ( factorial( i ) )

# write Fibonacci series up to n
def fib(n):    
a, b = 0, 1
while b < n:
  print b
  a, b = b, a+b

def cube(n): return n*n*n
def cubes(n):
  for i in range(1, n+1):
    print "%d" % (cube(i))

def main():
  nr = int(input("Enter a number: ")
  factorial(nr)         
  fact(nr)
  cubes(nr)

main()
#/Python27/python
def阶乘(n):

如果n问题源于没有足够的括号:

def main():
  nr = int(input("Enter a number: "))
  ...
您忘记了
int()

要在表中显示输出,我需要从每个函数返回一个列表,然后主要执行以下操作:

import itertools
print "Factorial up to {n}\tFibonacci of 1 to {n}\tCubes of 1 to {n}".format(n = nr)
print '\n'.join('\t'.join(map(str, seq)) for seq in itertools.izip_longest(factorial(nr), fib(nr), cubes(nr), fillvalue=''))
现在,如果每个函数(分别)返回以下列表:

>>> factorial(nr)=> [1, 2, 3, 4]
>>> fib(nr)=> [3, 4, 5, 6, 7]
>>> cubes(nr)=> [7, 453, 23, 676]
使用上述方法将产生如下输出:

Factorial up to -inf    Fibonacci of 1 to -inf  Cubes of 1 to -inf
1   3   7
2   4   453
3   5   23
4   6   676
    7   

它看起来不太像一个表,但是如果你在输出中填充更多的制表符,你应该得到一些更接近表格式的东西,你不需要在Python2中执行
int(input())
。XY你可以使用
n**3
而不是
n*n*n
,那么出了什么问题?错误?输出?如果是,输出是什么?是的,我正试图从main调用这三个函数,它在main函数中的阶乘(nr)处给出了一个错误:它表示无效语法。
int(输入(“输入一个数字”)
您缺少一个最终的
@ashishnitinpail但强制转换为int可确保在用户未输入int时在该行上引发直观错误。