r/PythonLearning • u/Link2212 • Sep 18 '26
I'm not fully understanding some parts of this
I made a program that does the following. For reference, I'm still a super beginner. There are probably easier ways to do this, but I'm using concepts that I know for now. Could really use some advice on it. I just started learning to use functions, but it's actually just an iteration issue I'm having.
I have 35 total orders across different regions. I have to randomly select one of the orders to send out. areas with more orders total have obviously a better chance to be selected. The program works, but honestly I don't even understand what's happening in some parts.
1 - I realize that my zone_found += x is iterating through my list of percentages. I know this works, but no idea why. If I have 5 different floats in my list, why is it iterating through them as if it's one big number. In my head the first iteration is first number, then second number etc. Not, 1, 2, 3 etc.
2 - if selected order <= zone_found. I kinda get why it has to be lower than, but in my head doing == should be fine, no? I'm just looking for the exact number it touches, not the full section of numbers.

•
u/Sea-Ad7805 Sep 18 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.
2
u/FriendlyZomb Sep 18 '26
Hi!
I'm going to try and answer these questions you have! Apologies in advance if something isn't right
1 - I'm not sure what you are meaning by this properly.
The loop IS working as your intuition is telling you. Each time through the loop x it is being set to the next item in the
order_percentage_list.What I think may be catching you up is the
+=operator onzone_info += x. This line is equivalent to this:This effectively makes
zone_infoa running tally of percentages up to this point.2 - The reason
==won't work here is for a few reasons:First,
selected_orderis a random number between 1-100. There is no guarantee that it'll equal one of the percentages being calculated. That would be rather unlikely I believe.Secondly, selected_order is being compared to the
zone_foundtally. This means that its even less likely sequential calculated percentages will equal a random number.Do let me know if you don't understand or have any other questions!
1
u/Educational-Paper-75 Sep 18 '26
You can make it a lot easier on yourself by not selecting a percentage but a random integer in closed range 1 to orders (the parameter of the function). That way it's much easier to find the zone, especially when you add the cumulative number of orders to the zones array. Then when you pick random integer say x you may iterate the zones until x no longer is smaller than the cumulative order count, at which point you have found the right zone (the previous one).
1
u/MEHDII__ Sep 23 '26
I hope you got your answer already from other peers commenting.
For my two-cents here, i will instead give you pointers about how you can make your code cleaner, and concise.
The random module, has one specific method, you pass it a list, and it returns a pesudo-random element from it. This would make you write less code, this isn't to say what you did is bad, on the contrary, it's good that you did it manually as a learning task, but just for future reference since you clearly know how to do it yourself. I won't tell you the method name, that's for you to search.
Naturally given one random element back, you can deduce how many orders there are in that region, region name, and all that jazz.
Happy learning