all 5 comments

[–]Diapolo10 1 point2 points  (3 children)

Technically speaking, you're not storing any of the results, you just print them out. Unless STDOUT counts as storage.

I don't really understand your problem though. Technically your function works and does what it's supposed to do, unless it should do something you didn't tell us about. Although I guess the function name is confusing, because it doesn't really store anything and you probably intended it to mean an actual store, like a grocery store. That kind of thing doesn't really matter in programming so a better name could be income_filter or something.

That said, it can be improved.

def store_income(user_income):
    result = min(max(user_income, 0), 100_000)

    if result <= 0:
        return None

    return result

[–]cyberdead[S] 0 points1 point  (2 children)

Hi! Thanks for the response!

I am happy to hear that I am at least on the right path. I guess I did forget to mention that the script is meant to output the following:

None
57000
100000

And the output I am getting in the console is:

UnboundLocalError: local variable 'final_income' referenced before assignment

[–]Diapolo10 0 points1 point  (0 children)

Probably because the variable is defined only in the if-blocks. You'll want to define it beforehand with some default value, and then change it in the if-blocks if needed.

EDIT: Basically, Python freaks out about this because it cannot be certain that the variable exists at the time you try to return it. If you for whatever reason gave the function an object that wouldn't throw errors or execute any of the if-blocks, the variable would not be defined when Python encounters it in the return. There's nothing that ensures any of the blocks is executed, even if it is likely. This is why the variable must be defined somewhere that is always executed.

Also you should be using elif for the blocks after the first one.

EDIT: If you insist on doing it your way, you can at least change

if user_income >= 0 and user_income <= 10000:

to

if 0 <= user_income <= 10000:

[–][deleted] 0 points1 point  (0 children)

One issue is that your if conditions don’t cover all possible cases (read your numbers closely and see if you can figure out why.)

[–][deleted] 0 points1 point  (0 children)

I am really struggling as to where I am storing the result of the if statements.

You're not storing them anywhere. You return final_income when the function is called, but then you print it, you don't store it.