I want to print the sequence in line with the format …How?
Question:
okay, I’ve figured out how to make the sequence result in one line, now I have to add a str…
here is the expected output:
Enter n: 10
Fibonacci numbers = 1 1 2 3 5 8 13 21 34 55
here is my code:
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("Fibonacci numbers = ")
print(fib(i), end=" ")
here is the output that I want to fix:
Enter n: 10
Fibonacci numbers = 1 Fibonacci numbers = 1 Fibonacci numbers = 2 Fibonacci numbers = 3 Fibonacci numbers = 5 Fibonacci numbers = 8 Fibonacci numbers = 13 Fibonacci numbers = 21 Fibonacci numbers = 34 Fibonacci numbers = 55
I’m still learning python independently, Thank you to those who’ll take time to answer..
Answers:
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
print("Fibonacci numbers:")
fib_list = list()
for i in range(int(n)):
fib_list.append(str(fib(i)))
print(' '.join(fib_list))
If you don’t want "Fibonacci numbers: "
to print out on each loop iteration, remove it from the loop body.
print("Fibonacci numbers = ")
for i in range(10):
print(fib(i), end=" ")
To do the least calculation, it is more efficient to have a fib
function generate a list of the first n
Fibonacci numbers.
def fib(n):
fibs = [0, 1]
for _ in range(n-2):
fibs.append(sum(fibs[-2:]))
return fibs
We know the first two Fibonacci numbers are 0
and 1
. For the remaining count we can add the sum of the last two numbers to the list.
>>> fib(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
You can now:
print('Fibonacci numbers = ', end='')
print(*fib(10), sep=' ', end='n')
okay, I’ve figured out how to make the sequence result in one line, now I have to add a str…
here is the expected output:
Enter n: 10
Fibonacci numbers = 1 1 2 3 5 8 13 21 34 55
here is my code:
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("Fibonacci numbers = ")
print(fib(i), end=" ")
here is the output that I want to fix:
Enter n: 10
Fibonacci numbers = 1 Fibonacci numbers = 1 Fibonacci numbers = 2 Fibonacci numbers = 3 Fibonacci numbers = 5 Fibonacci numbers = 8 Fibonacci numbers = 13 Fibonacci numbers = 21 Fibonacci numbers = 34 Fibonacci numbers = 55
I’m still learning python independently, Thank you to those who’ll take time to 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
print("Fibonacci numbers:")
fib_list = list()
for i in range(int(n)):
fib_list.append(str(fib(i)))
print(' '.join(fib_list))
If you don’t want "Fibonacci numbers: "
to print out on each loop iteration, remove it from the loop body.
print("Fibonacci numbers = ")
for i in range(10):
print(fib(i), end=" ")
To do the least calculation, it is more efficient to have a fib
function generate a list of the first n
Fibonacci numbers.
def fib(n):
fibs = [0, 1]
for _ in range(n-2):
fibs.append(sum(fibs[-2:]))
return fibs
We know the first two Fibonacci numbers are 0
and 1
. For the remaining count we can add the sum of the last two numbers to the list.
>>> fib(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
You can now:
print('Fibonacci numbers = ', end='')
print(*fib(10), sep=' ', end='n')