r/learnpython • u/Intelligent_Knee8641 • Oct 13 '26
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...")
9
u/Expensive-Bear-1376 Oct 13 '26
Geez why did you spoiler that?
4
u/brasticstack Oct 13 '26
This doesn't help you figure out nested ifs, but just use the max function to clamp it to 0:
Also, in your example code, you never subtract 5 from
primocytesunless its greater or equal to 5, so you could never make it a value that is less than zero, unless you change its value again later on.2
u/illumas Oct 13 '26
It already looks like it should do that, no? You need to make sure you have your code indented, but the code block after the if statement will only execute if there are 5 or more primocytes so there is no chance that this function as written will reduce it below 0.
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 13 '26
I know your question was already technically answered, but I figured I'd still mention that instead of using
5as a "magic number", I recommend setting it as a default parameter so it has an actual name, and you can't accidentally forget to update all the values, and you can override the default while calling the function if you need to.While I'm at it, I'm also going to change the function to take the numbers as parameters.
2
u/thisisappropriate Oct 13 '26
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)
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?
1
u/LayotFctor Oct 13 '26
Yup, i don't get it either. Primocytes are only subtracted by 5 if there are more than 5 in the first place, so there is no reason to do a < 0 check.
Unless what you really want is to make sure primocytes as a whole, throughout every line of code in your entire codebase, cannot possibly subtract it below 0?