#đź”’ Beginner confusion
45 messages · Page 1 of 1 (latest)
@void sentinel
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.
You didn't say to print(f100)
You only have print(fib(0)) so you only see what that print printed
!indent
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
you return the result on the first iteration
also well, your 2 screenshot is not continuous
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)
A trace for you, if it is helpful
!d yield
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...
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
Does return break the loop so I only get the result of 0?
the moment any function hits the return keyword, it immediately stops and returns the requested value. Your while loop completely stops during teh first loop, therefore you only append a single value of 0 to your list and return that
Ohhh okay
yes, return returns exits the funciton
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
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?
did you save the file before running it?
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
just keep in mind that using yield instead of return here will not achieve the behaviour that the docstring mentions
Ohh okay
I saved and it worked my bad
Or refreshed
!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))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[3]
yeah that's what I would expect 🙂
one change to that code will give you the list you're after
Thank you
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)))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Btw is it a good way to define a fib function?
I was told that fib should be best defined the calling the function in the defination , using fib(n) , if n > 2 , return fib(n-1) + fib(n-2)
that looks like functional programming where you use recursion a lot more as variables are immutable in strictly functional languages
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
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.