[Python] I want to have the Fibonacci sequence returned with commas between the numbers

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    def get_fibonacci_sequence(num: int) -> str:
    '''
    Function to return string with fibonacci sequence

    >>> get_fibonacci_sequence(0)
    ''
    >>> get_fibonacci_sequence(1)
    '0'
    >>> get_fibonacci_sequence(9)
    '0,1,1,2,3,5,8,13,21'
    '''

    first_term = 0
    second_term = 1
    nth_term = 0
    result = ''

    for sequence in range(num):
    result += str(first_term)
    nth_term = first_term + second_term
    first_term = second_term
    second_term = nth_term
    return(result)


    I tried adding (+ ',') in the loop where I convert the numbers to a string and it works but not the way I want it to.

    '0,1,1,2,3,5,8,13,21,'

    I want to get rid of the comma after 21

    Continue reading...

Compartilhe esta Página