-1

I'm very new to python and programming in general. This is the first year we have programming classes, so I'd say I started about a month ago. We got our first assignment last week and I've managed to make most of the tasks, apart from the following one:

Generate n numbers from the Fibonacci sequence, where n is entered by the user. Write these numbers to the screen.

Print the numbers on 1 single line.

Tip: print (value, end = '')

Do not use list or array.

This is the last question and this one is significantly more difficult than the others. I tried some things but non came close to doing what is asked. I genuinely have no clue how I'd even have to start.

Tony
  • 9,672
  • 3
  • 47
  • 75
ZinoWR
  • 23
  • 2
  • Does https://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence?rq=1 answer your question? – Uvar Nov 18 '20 at 16:46
  • are you allowed to use recursion? – Pablo C Nov 18 '20 at 16:48
  • @PabloC It doesn't say it isn't, but we haven't seen it yet. I don't think this is a problem tho, so I'd say yes – ZinoWR Nov 18 '20 at 16:56
  • @Uvar the first couple of answers there don't answer mine I think, I'm currently looking through the others – ZinoWR Nov 18 '20 at 17:00
  • What is your question exactly? How to calculate Fibonacci numbers, or how to print them all in one line? (both have lots of existing answers on this site) – Tomerikoo Nov 18 '20 at 17:02
  • 2
    Does this answer your question? [How do I print a fibonacci sequence to the nth number in Python?](https://stackoverflow.com/questions/15820601/how-do-i-print-a-fibonacci-sequence-to-the-nth-number-in-python) followed by [print in one line dynamically](https://stackoverflow.com/questions/3249524/print-in-one-line-dynamically) – Tomerikoo Nov 18 '20 at 17:06
  • 2
    You're never going to learn if you get others to write your code for you. Moreover, "do my homework for me" is off-topic here. [How do I ask and answer homework questions?](//meta.stackoverflow.com/q/334822/843953) Please read [ask] and [what's on-topic](/help/on-topic). Then _make an honest effort_, and if you run into a problem, provide a [mre] that reproduces _the specific problem you're having with your code._ Welcome to Stack Overflow! – Pranav Hosangadi Nov 18 '20 at 17:27

4 Answers4

1

In pseudocode:

  1. First you need to check, print and save the results of fibonacci(0) and fibonacci(1)
  2. Then make a loop to calculate the next fibonacci value using the last two calculated and print it.
  3. Update the variables for the last two calculated.
  4. Iterate steps 2 and 3 for each N>2

Thinking in python3:

# Print Fibonacci(0)
if n>=0:
    print (1, end =', ')
    f2 = 1
# Print Fibonacci(1)
if n>0:
    print (1, end =', ')
    f1=1
# Print Fibonacci(n) ; n>1

if n>1:
    for i in range (2, n+1):
        result = f1+f2
        print(result, end=', ')
        # updating last two variables
        f2 = f1
        f1 = result
L.Stefan
  • 341
  • 1
  • 14
0

You need to think about loops, how to iterate a set number of times, and use the print statement given to you in the "tip".

In pseudo code:

next_in_sequence = 1
loop_count = 1
while loop_counter <= number_entered:
    next_in_sequence = caclulate_next_fibonacci(next_in_sequence)
    print (next_in_sequence, end = '')
    loop_counter = loop_counter + 1

Have a go at writing this in python, you'll have to write the caclulate_next_fibonacci function to work out the next number in the sequence. If you have any problems edit your question to show your code and ask for more help

Tony
  • 9,672
  • 3
  • 47
  • 75
0

Although not mentioned, you may want to 'memorize' previous Fibonacci numbers so you can generate the sequence faster.

def fibonacci(n, _cache={}):
    if n in _cache: # Check to see if the number has been done
        return _cache[n] # Return the pre-done number
    elif n > 1:
        return _cache.setdefault(n, fibonacci(n-1) + fibonacci(n-2)) # Call this function recursively to generate the previous Fibonacci numbers
    return n

n = int(input("n = "))
for i in range(n):
    print(fibonacci(i), end=', ')
superTyDev
  • 26
  • 5
  • Why does he need memoization if he anyway needs to calculate all numbers up-to `n`? – Tomerikoo Nov 18 '20 at 17:32
  • It isn't necessary, but I thought it helpful for actual usage if you want to generate large numbers (beyond around 30). – superTyDev Nov 18 '20 at 17:35
  • 1
    Again, the assignment is to print ***all numbers*** up to `n`, not just a specific random `n` so it's not really making any difference (you can just loop and print the numbers at the same time) – Tomerikoo Nov 18 '20 at 17:36
-1

To calculate the next Fibonacci number simply add together the two previous values. You can do this in a for loop using variables declared outside the loop to hold the previous two values.

Use this statement within your loop

print (value, end = '')

This will print the sequence all on one line with each value separated by a space.

n = int(input()) 

fib0 = 0
fib1 = 1
for x in range(0, n):
    nextFib = fib0 + fib1
    fib0 = fib1
    fib1 = nextFib
    print(fib0 , end = ' ')  
Stacey
  • 281
  • 1
  • 11
  • 1
    this does it, thanks :D – ZinoWR Nov 18 '20 at 17:08
  • 3
    Code-only answers aren't helpful, especially to "gimme the codez" homework questions that do not demonstrate any attempt at solving the problem. [How do I ask and answer homework questions?](//meta.stackoverflow.com/q/334822/843953) You should add an explanation to improve your answer. – Pranav Hosangadi Nov 18 '20 at 17:28