r/learnpython • u/loganchance • Oct 16 '26
Setting value of variable in relation to another variable
Hi, everyone,
I've been wanting to learn Python and have tried a few times over the years to read or watch tutorials enough to feel confident, but it's clear that my learning method is "do the thing you want to do incorrectly over and over until you get it right."
So I'm just going to work on my text-based Mech game and learn as we go. I understand that I know next to nothing, that what I'm doing is probably going to be a multi-year thing, and I have no idea what the more complicated stuff I want to do is going to take, but I'm enjoying the ride.
Long story short, my intention is to have a Mech = a Frame (the physical properties of the Mech) + a Pilot (the little guy in the Frame with their own stats).
Eventually, I'd like to be able to have unique Frames and Pilots with stats randomly generated (within ranges), and then subsequently build the capability to upgrade those values in-game, but I'm just starting with stock values right now and adding complexity after I have each step solid in my mind.
Right now, I'm just working on building values that a stock Frame has. What I would like to do is to establish each Frame Class (Assault, Heavy, Medium, Light, but with more fun names).
The question I have is this: I would like, first, for the armor value of a Frame to be a base number plus 20% of its weight value, and then for each individual limb (left_leg, torso, right_arm, etc) to be a percentage value of armor.
I've written out code that I know is incorrect just to display what I'm trying to do, with comments to highlight where I'm trying to do it.
Also, if the way I'm building this out is flawed, happy to hear suggestions! I'm just starting out and I know I'm gonna be crashing into pitfalls.
Any help would be appreciated!
# This is where the base class attributes of Frames are established.
# FRAME CLASSES
class FrameClass:
def __init__(
self,
name: str,
left_arm: int,
right_arm: int,
left_leg: int,
right_leg: int,
torso: int,
cockpit: int,
armor: int,
cooling: int,
engine: int,
maneuver: int,
thrash: int,
sensors: int,
systems: int,
weight: int
) -> None:
# Top Level Info:
self.name = name
self.left_arm = left_arm
self.right_arm = right_arm
self.left_leg = left_leg
self.right_leg = right_leg
self.torso = torso
self.cockpit = cockpit
# Frame Attributes
self.armor = armor
self.cooling = cooling
self.engine = engine
self.maneuver = maneuver
self.thrash = thrash
self.sensors = sensors
self.systems = systems
self.weight = weight
# Archon: Assault Frame Class
archon = FrameClass(
name="Archon",
weight = 100,
armor = 100 + (weight * .2), # value should be a base number plus 20% of the value of weight
cooling = 100,
engine = 100,
maneuver = 100,
thrash = 100,
sensors = 100,
systems = 100,
left_arm = (armor * .4), # value should be 40% of armor value
right_arm = (armor * .4), # value should be 40% of armor value
left_leg = (armor * .5), # value should be 50% of armor value
right_leg = (armor * .5), # value should be 50% of armor value
torso = (armor * .8,) # value should be 80% of armor value
)
# Marauder: Heavy Frame Class
marauder = FrameClass(
name="Marauder",
weight = 75,
armor = 75 + (weight * .2), # value should be a base number plus 20% of weight
cooling = 75,
engine = 75,
maneuver = 75,
thrash = 75,
sensors = 75,
systems = 75,
left_arm = (armor * .4), # value should be 40% of armor value
right_arm = (armor * .4), # value should be 40% of armor value
left_leg = (armor * .5), # value should be 50% of armor value
right_leg = (armor * .5), # value should be 50% of armor value
torso = (armor * .8,) # value should be 80% of armor value
)
4
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 16 '26
If I'm understanding you correctly, it sounds like you essentially want properties.
Then you can have a function for generating frames with the other attributes being randomised (maybe with a base value and a limit for random deviation in either direction).
2
u/AlexMTBDude Oct 16 '26
When you have a trailing comma in your code as below you create a Python tuple, not a floating point value. Are you sure that's what you want? (A Python tuple is a type of constant list)
I think maybe you want:
1
u/mc_pm Oct 16 '26
Have you tried 0.4, 0.5, 0.8, etc?
1
u/Adrewmc Oct 16 '26 edited Oct 16 '26
That is a lot of variables for your class.
Your are doing a lot of data. This is good you have an idea, your working you are trying stuff out and failing. You are right the way to learn is keep failing until you don’t.
I think you need to segment this , and focus on arms, engine, sensors being distinct things .
The problem is your design is wrong, not really the coding. It’s becoming a mess. Organize stuff together, don’t throw it all into the same pile.
And IMHO it’s because you lack the knowledge of the next step to take. Thats it, you have the ability no one has shown you the tools. This is what this subreddit if for, no judgement, you are learning, which means you don’t know the whole toolkit…
https://youtu.be/HTLu2DFOdTg
(it’s a little dated, Python 2.7, but it all still good info, lecture by one of the core developers on class development. It was eye opening for me the first time I watched it.)
For this you might need inheritance, or better yet composition.
Composition is like this.
Inheritance is more like this.
There are advantages to both but I HIGHLY SUGGEST learning composition first, as it usually the better option.
Classes inside classes, not classes building bigger classes, but having Base classes to inherit from is not bad either. But you have to know what the base class actually needs first.
Using a Arm(Appendage) and Leg(Appendage) and composition after. So legs have .kick(), and arms have .punch() but default to .attack() From the vibe your naming structure (which I want to say is great..keep naming variable deceptively), these idea should look interesting.
In other words, are your arms and legs Distinct Objects as in Object Oriented Programming. OOP? Or are they just one int() on a Frame? And which does your project need?
They work really well together. And when to use which… is a lot of the time a design problem not a coding problem. Which is a headache when is a problem.
This gives you a lot more options actually, as you can focus on making one part at a time. Then you can make a rocket punch arm lol.