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
)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)
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 valueI think maybe you want:
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 value2
u/loganchance Oct 16 '26
That's it, thank you, it was becoming clear I was missing something obvious!
1
u/mc_pm Oct 16 '26
Have you tried 0.4, 0.5, 0.8, etc?
1
u/loganchance Oct 16 '26
I haven't, though that's probably good practice even if .4 works. But what it's not liking is the "armor" in "armor * 0.4" and I'm unsure how one goes about that.
3
u/cdcformatc Oct 16 '26 edited Oct 16 '26
what's wrong with
armor * 0.4? if you want one value to be relative to another, with some scaling factor, there's no other way to do itinside the class
__init__function define the other variables relevant to the parameters passed inclass FrameClass: def __init__( self, name: str, weight: int, ... # other parameters here ) armor = 75 + weight * .2 left_arm = armor * .4 right_arm = armor * .4 left_leg = armor * .5 right_leg = armor * .5 torso = armor * .81
u/loganchance Oct 16 '26
It's kicking back " 'weight' is not defined," same for all uses of "armor" in this manner
1
u/mc_pm Oct 16 '26
I was so hung up on the decimal that I didn't notice it. Yeah, those are all instance variables -- the ones for that object alone -- so you need to use self:
self.armor = 75 + self.weight * 0.2
(And you'll need to move those into the init method)
1
u/cdcformatc Oct 16 '26
move the definitions into the
__init__methodclass FrameClass: def __init__( self, name: str, weight: int, ... # other parameters here ) self.weight = weight self.armor = 75 + self.weight * .2 self.left_arm = self.armor * .4 self.right_arm = self.armor * .4 ... etc ...
1
u/Adrewmc Oct 16 '26 edited Oct 16 '26
from dataclasses import dataclass
@dataclass
class Frame:
“””because to mention dataclasses.”
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
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…
(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.
“””Simple semi-related Composition Example.”””
class Arm:
“Basic arm for a Frame”
def __init__(self, armor, weight):
self.armor = armor
self.weight = weigh
self.HP = armor + weight
def attack(self, *args):
return self.weight * 0.4
def defend(self, damage):
dmg = damage - armor
self.HP -= dmg if dmg > 0 else 0
self.armor -= 1
return self.HP
class Frame:
“”” class Arm() can be made into left and right Arm()s”””
def __init__(self, name, armor, weight):
self.armor = armor
self.weight = weight.
self.arm= Arm(armor, weight)
self.left_arm = Arm(armor, weight)
…
bob = Frame(“Bob”, 6, 7)
print(bob.arm.attack())
print(bob.left_arm.defend(2))
Inheritance is more like this.
“””Simple semi-related Inheritance Example.”””
class Frame:
def __init__(self, name, armor, weight):
self.armor = armor
self.weight = weight
def attack(self):
return int(self.armor * 0.4)
def defend(self, damage):
return damage - self.armor
class HeavyFrame(Frame):
“””Uses Frame’s __init__()”””
def attack(self):
#overwrites Frame.attack()
return int((self.weight + self.armor)*0.4)
Biff = HeavyFrame(“Biff”, armor, weight)
print(Biff.attack())
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.
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).