Home > Net >  How to print the Fibonacci sequence in one line using python?
How to print the Fibonacci sequence in one line using python?

Time:10-18

I just need to print the sequence in one line, how do I do that? Thanks to those who'll answer

n = input("Enter n: ")

def fib(n):
    cur = 1
    old = 1
    i = 1
    while (i < n):
        cur, old, i = cur old, cur, i 1
    return cur

for i in range(10):
    print(fib(i))

CodePudding user response:

Add a argument in print() function:

print(fib(i), end=" ")

CodePudding user response:

by default, the print function ends with a newline "/n" character

You can replace that with whatever character you want with the end keyword argument

e.g.

print(fib(i), end = " ")
  • Related