斐波那契序列不一致

时间:2014-11-04 12:00:37

标签: fibonacci

我做了一个简短的程序来生成用户指定长度的斐波纳契序列。当我运行代码并输入一个像6这样的数字时,它将显示如下序列:

1,1,2,3,5,8,

当字符串停留在一行时,如何摆脱初始空格?下面是我的代码

#user intiger input
print("\n")
f = int(input("Enter length of sequence: "))
print("\n")


f1 = 1
f2 = 1
multiply = 2


if f <=0:
   print("Enter a positive integer for the length: ")


elif f == 1:
   print("The Fibonacci sequence: ")
   print("\n")
   print(f1,end=',')

else:
   print("The Fibonacci sequence: ")
   print(f1,",",f2,end=",")
   while multiply < f:
       f3 = f1 + f2
       print(f3,end=",")

       f1 = f2 
       f2 = f3 
       multiply += 1

print("\n")

1 个答案:

答案 0 :(得分:1)

else:
print("The Fibonacci sequence: ")
print(f1,end=",")
print(f2,end=",")

根据我的观点,这是最简单的解决方案.. 但通过看到你的代码似乎输出应该摆脱初始spce 但是根据suggetion&amp;让我知道输出

相关问题