【发布时间】:2017-11-27 13:46:52
【问题描述】:
所以我正在制作一个程序,它打印斐波那契数列中从 1 到给定输入的每个值,以及我所在的一个类。他希望我们做到这一点,以便在打印输出时,它们是全部用双破折号分隔。例如:1 -- 1 -- 2 -- 3 -- 5 等等。我的问题是如何让它做到这一点而不是输出值之间的逗号? 请不要说我的代码有多长,但这是我目前所拥有的: t = 假
def comma(num):
if type(num) == int:
return '{:,}'.format(num)
elif type(num) == float:
return '{:,.2f}'.format(num)
else:
print("Need a number to function comma")
def fib(limit):
numbers = ["0","1"]
x3 = 1
x2 = 1
x1 = 1
while x3 <= int(limit):
if x3 >=999:
n = [(comma(x3))]
numbers = numbers + n
x1 = x2
x2 = x3
x3 = x2 + x1
return(numbers)
def isint(value):
try:
int(value)
return True
except ValueError:
return False
def printFib():
b = False
while b == False:
limit = input("What is the limit value?")
if isint(limit):
2 + 2
if int(limit) >= 1 and int(limit) != 1 and int(limit) <= 8944394323791464:
b = True
else:
if int(limit) == 0 or int(limit) == 1:
print("really it can't start.")
elif int(limit) >= 8944394323791464:
print("Too big, just too big.")
else:
print("Use positive numbers above one, or I will tell Antonio Montoya that you killed his father and he will find you.")
else:
print("Use whole numbers.")
print(str(fib(limit)))
if input("Try again y/n?") == "y":
printFib()
printFib()
【问题讨论】:
-
我最喜欢的线路:
2 + 2 -
您应该将您的代码缩减为 MCVE:stackoverflow.com/help/mcve
-
@GPhilo 减一等于三,快速计算!
-
考虑使用
'--'.join()方法。