0

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))
2
  • You really want to recalculate the whole sequence for every item? Commented Oct 18, 2022 at 3:26
  • I need to display the sequence in one line but I don't know how.. Commented Oct 18, 2022 at 3:28

2 Answers 2

2

Add a argument in print() function:

print(fib(i), end=" ")
Sign up to request clarification or add additional context in comments.

Comments

2

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 = " ")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.