I've made it ever so slightly better by adding guard clauses since.
I'm trying to make a Dwarf Fortress clone, and for crafting I needed some items to be of the same MaterialType, because they'd have the same function.
Example: Oak Wood and Cherry Wood are both of the MaterialType Wood, and in a crafting recipe, a wooden item would typically require X items of the MaterialType Wood.
So, take the Carpenters' Table, which is a workshop. It requires 3 items of the MaterialType Wood.
The snippet of code is useful when you want to build the Carpenters' Table, and there is only 2 Oak Wood and 1 Cherry Wood available. It will automatically take the 2 Oak Wood and then the 1 Cherry Wood, leaving you without any item of the MaterialType Wood.
just to check: you do realize your outer loop will never break? you'd have to either break using labels to break out of all 3 loops, or you'd have to modify either side of the condition in some way, but it doesn't seem to be doing either of those
the variable needed is first, very badly named as I was tired yesterday, as it actually represent the total count of said MaterialType minus the amount that is required by the recipe, I should've included that in the post
The only thing that matters is that you keep your conventions consistent within your project. That said, it's traditional to use snake case in python projects (and recommended by PEP8) so if you end up working in a python codebase with others be prepared to adjust.
while colony.GetCountByMaterialType()[item] > needed:
for invItem in inventory.keys():
if addedItems < count:
if invItem.materialType == item:
while inventory[invItem] > 0:
inventory[invItem] -= 1
addedItems += 1
if addedItems < count:
continue
else:
break
This isn't catastrophically bad. I can see what this is trying to do without much thinking, but it'd need a proper function name to be perfect in that regards. Functionally, you could combine the two if's with an and or just combine the condition of the last if with its while's condition and not even need the first if
Resulting in: while ... for ... if 2nd condition while ... & 1st condition
I'd definitely extrapolate some of this code into some smaller functions. For example, the final while loop could be converted into a remove_max_items function of an inventory object. It could take the item to remove and the number to attempt to remove. It'd handle the logic to remove the max number of item it could take, and return the number of items that are still needed.
20
u/rgnkn Dec 20 '22 edited Dec 20 '22
@ OP:
Please send me your address as I'm going to sue you!
I've just got blinded and mentally destroyed by your post.
[EDIT]
Is this really the whole loop? Isn't there something missing? What is it supposed to do? Are there any slight chances that this might be a bit buggy?
I just don't get it.
3
u/UndisclosedChaos Dec 20 '22
camelCase and Python don’t sit very well in my stomach. But that’s cool you’re making a game!
6
u/Full-Inevitable-3229 Dec 20 '22
I’m all about that O(n)3
5
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 20 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/floofyb Dec 20 '22 edited Dec 20 '22
This isn't catastrophically bad. I can see what this is trying to do without much thinking, but it'd need a proper function name to be perfect in that regards. Functionally, you could combine the two if's with an and or just combine the condition of the last if with its while's condition and not even need the first if
Resulting in: while ... for ... if 2nd condition while ... & 1st condition
2
u/TheSapphireDragon Dec 21 '22
When you're allergic to functions
2
u/dumassonreddit Dec 21 '22
Excuse me sir, you're missing another loop in the loop in a loop in a loop 😑
2
u/07Loki Dec 21 '22
If nested If's are wrapped and called AI,
we need a new Concept for nested loops
Agree or not ???
1
u/freezydrag Dec 20 '22
I'd definitely extrapolate some of this code into some smaller functions. For example, the final
whileloop could be converted into aremove_max_itemsfunction of an inventory object. It could take the item to remove and the number to attempt to remove. It'd handle the logic to remove the max number of item it could take, and return the number of items that are still needed.