【问题标题】:Fibonacci series printing horizontally [duplicate]斐波那契数列水平打印[重复]
【发布时间】:2012-08-20 17:47:27
【问题描述】:

可能重复:
Printing without newline (print 'a',) prints a space, how to remove?

我需要水平打印斐波那契数列..即,输出必须是这样的 斐波那契数列是:0,1,1,2,3,5,8,13。 我也不想将其打印为列表。 我知道如何垂直打印..但不能用“斐波那契数列是:”水平打印一次..请大家帮忙!!

【问题讨论】:

标签: python-2.7


【解决方案1】:

我假设您希望将系列中的所有数字打印在同一行上。

您的代码将如下所示

print 'The Fibonacci series is : ',
for i in xrange(1,10):
    #Calculate the next number 'n' to print
    print n,

【讨论】:

    【解决方案2】:

    基于 Rajesh 答案的工作解决方案。

    a,b = 0,1
    limit = 40
    print 'The Fibonacci series is :', str(a), #str() necessary since 'print 0,' == ''
    while(b < limit):
        print b,
        a,b = b,a+b
    
    # output:
    # The Fibonacci series is :  0,1,1,2,3,5,8,13,21,34
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 2013-04-11
      • 1970-01-01
      • 2022-11-27
      • 2021-06-14
      相关资源
      最近更新 更多