【问题标题】:How do i place a double dash between values in an output (I'm using Python)?如何在输出中的值之间放置双破折号(我正在使用 Python)?
【发布时间】: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() 方法。

标签: python fibonacci


【解决方案1】:

替换:

print(str(fib(limit)))

print('--'.join(fib(limit)))

您可能希望在 -- 周围添加空格(您说 由双破折号分隔,但您的示例显示带有空格的双破折号)。

另外,在函数fib()中你需要初始化n,可能是[]

【讨论】:

    【解决方案2】:

    这对我有用:

    def fib(limit):
        numbers = ["0","1"]
        x3 = 1
        x2 = 1
        x1 = 1
        while x3 <= int(limit):
            numbers.append(str(x3))
            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 = raw_input("What is the limit value?")
            if isint(limit):
                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('--'.join(fib(limit)))
        if raw_input("Try again y/n?") == "y":
            printFib()
    
    printFib()
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 2012-01-15
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多