r/learnpython • • 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...")

0 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/Intelligent_Knee8641 Oct 13 '26

Wow thank you for the catch and teaching me a new function, I didn’t quite think that through before I asked I guess.. But I am pretty new to this lol.

4

u/zaphodikus Oct 13 '26

Also, try to stop using the 'global' keyword, it leads to errors in larger programs. Once you get nesting worked out, read up on how to pass 'parameters', and how to create 'classes'.

2

u/Intelligent_Knee8641 Oct 13 '26

Thank you for the tip I will look into it, I know that’s a next step to make this better but I was just trying to test something for a function and don’t know how to do it otherwise yet. I originally didn’t have the ‘global’ keyword in the code but without it, I was having an error because the two variables were outside the if statement and I didn’t know how else to fix it because I want those values stored outside the if for state management

2

u/zaphodikus Oct 13 '26

No worries, don't rush, work at your speed, mostly have fun.