#đź”’ Beginner confusion

45 messages · Page 1 of 1 (latest)

void sentinel
#

Why is the result printed as only [0]? Isn’t this code supposed to “return” a sequence of the Fibonacci until it is greater than n, or am I misunderstanding the return function too

hidden jacinthBOT
#

@void sentinel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

near aurora
#

You didn't say to print(f100)
You only have print(fib(0)) so you only see what that print printed

tough wraith
#

!indent

hidden jacinthBOT
#
Indentation

Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.

Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.

Example

def foo():
    bar = 'baz'  # indented one level
    if bar == 'baz':
        print('ham')  # indented two levels
    return bar  # indented one level

The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the if statement, and will only run if the if statement evaluates to True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the if statement above as no more lines can be inside the if statement below that line.

Indentation is used after:
1. Compound statements (eg. if, while, for, try, with, def, class, and their counterparts)
2. Continuation lines

More Info
1. Indentation style guide
2. Tabs or Spaces?
3. Official docs on indentation

tough wraith
#

you return the result on the first iteration

#

also well, your 2 screenshot is not continuous

near aurora
#

This isn't repl to print automatically everything that had a value. (the line saying just f100)
File script will only print what you explicitly tell it to print, hence only print(fib(0)) shows up

And after you fix f100 to be print(f100), that's when you'll get the exact same result and indent will matter (what Noping tells you)

tough wraith
#

A trace for you, if it is helpful

void sentinel
#

Is this an incorrect fix?

#

I know it feels like talking to a wall lol

fluid scarab
#

!d yield

hidden jacinthBOT
#

7.7. The yield statement


yield_stmt ::= yield_expression

A yield statement is semantically equivalent to a yield expression. The yield statement can be used to omit the parentheses that would otherwise be required in the equivalent yield expression statement. For example, the yield statements...

fluid scarab
#

i think this might be the one you want rather then return

#

oh, i see now that you are doing append and returning the list, forget what i just said

#

but if you are going to do return, you want to do it after the loop is done

#

so, outside of the loop

void sentinel
#

Does return break the loop so I only get the result of 0?

red jungle
void sentinel
#

Ohhh okay

fluid scarab
#

all you would need to do is unindent the return statement one position so that it is on the same level as the while keyword

void sentinel
#

When I change the value of a, b = 0, 1 to like = 3, 5 is there a reason it doesn’t return 3 and only 0 again?

red jungle
fluid scarab
#

yield that i mentioned before would have made your function a generator, which returns data for each time it hits the yield keyword and doesn't stop until the function ends in some other way

red jungle
#

just keep in mind that using yield instead of return here will not achieve the behaviour that the docstring mentions

void sentinel
#

Ohh okay

void sentinel
#

Or refreshed

red jungle
#

!e

def fib2(n):
    result = []
    a, b = 3, 5
    while a < n:
        result.append(a)
        a,b = b,a+b
        return result
print(fib2(100))
hidden jacinthBOT
red jungle
#

yeah that's what I would expect 🙂

#

one change to that code will give you the list you're after

void sentinel
#

Thank you

fluid scarab
#

just a demo of how yield would work in this situation

#

!e

def fib(n):
    a, b = 1, 1
    while a < n:
        yield a
        a, b = b, a + b
       
print(list(fib(100)))
hidden jacinthBOT
lime nest
fluid scarab
#

it would use up a lot more resources, especially in python

#

also, it's slower if not using memoization (caching of function return values dependent on the values of function arguments)

#

my preference in python would be to use a loop similar to the implementation you went with

hidden jacinthBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.