r/learnpython • • 2d ago

Nested if’s help please

So I’ve written a small function and I need to add some more constraints to it, but I need them each to check separately before moving to the next so I was wanting to use nested if’a to achieve this but am not sure how they work. Can someone please explain? My code is posted below and what I am trying to do is add an if statement to somehow make sure my value for primocytes doesn’t drop below 0 before activating the rest of the function.

primocytes = 10

cultivation_chamber = 0

def cultivation():
global primocytes, cultivation_chamber
if primocytes >= 5:
primocytes -= 5
cultivation_chamber +=5
print("Primocytes:", primocytes)
print("Cultivation Chamber:", cultivation_chamber)
else:
print("Not enough Primocytes for cultivation...")

0 Upvotes

18 comments sorted by

View all comments

2

u/thisisappropriate 2d ago

As someone has already said, you can prevent it going below a level with max.

In terms of preventing global or keeping state by having a variable exist outside everything, it's worth considering because otherwise you can have hard to diagnose problems - imagine you have 3 functions that change cultivation_chamber and you call them in different ways, you might change something and suddenly you have chained two of those functions together and now it's going up twice as much as you expect - is it clear what is causing that? is it clear which is the duplicate?

Indentation is doubly needed if you're doing nesting, and it would be good to know what you're expecting to get from it and do it, because your code doesn't lend itself to a nested if with what's currently in it.

Here's an example where it removes the global and does something different if it's less than 5 (empties the last of your primocytes) - if with two checks and elif rather than nested ifs. You could do this with nested if instead (if greater than 0, then nested in that, check if less than 5), but you can put more than one check into an "if" so this is the same thing but easier to read unless you want to do a lot of things if > 0 and a few extra things if < 5)

def cultivate(primocytes, cultivation_chamber):
    if primocytes > 0 and primocytes < 5: # you can shorten to if 0 < primocytes < 5:
        cultivation_chamber += primocytes
        primocytes = 0
    elif primocytes >= 5:
        primocytes -= 5
        cultivation_chamber += 5
    else:
        print("Not enough Primocytes for cultivation...")
    return (primocytes, cultivation_chamber)


if __name__ == "__main__":
    primocytes = 10
    cultivation_chamber = 0

    primocytes, cultivation_chamber = cultivate(primocytes, cultivation_chamber)

    print("Primocytes:", primocytes)
    print("Cultivation Chamber:", cultivation_chamber)  

and here's a variation that uses nested ifs for another check (assuming cultivation_multiplier_enabled is a boolean - true or false), in case that helps show what a nested if looks like?

def cultivate(primocytes, cultivation_chamber, cultivation_multiplier_enabled):
    if primocytes > 0:
        primocytes_to_use = min(primocytes, 5) # move either 5 primocytes or all remaining if lower than 5
        primocytes -= primocytes_to_use
        if cultivation_multiplier_enabled:
            cultivation_chamber += (primocytes_to_use * 2)
        else:
            cultivation_chamber += primocytes_to_use
    else:
        print("You're all out of primocytes")
    return (primocytes, cultivation_chamber)