Python Forum
needing some help to write some code for a game calculator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: needing some help to write some code for a game calculator (/thread-41367.html)



needing some help to write some code for a game calculator - rymdaksel - Jan-02-2024

i just want to preface this thread by saying that i am a complete noob with python. i started learning a few days ago, going off of only knowledge about functions in google sheets, and some basic 'python for beginners' tips on how to atleast write some lines.

im attempting to make a calculator for a game, it counts how many hits it takes to kill another player. so far, i only have the damage-afflicting part written. id like to know how i could make a line of code that recognizes if the damage value being output is more than double or half of the original damage value. if it is more than double or half, like 100 to 200, or 100 to 50, id like it to output double or half the value. ie 220 damage gets turned to 200, because its more than double of 100.

if i could get some help with this, thatd be greatly appreciated!


RE: needing some help to write some code for a game calculator - deanhystad - Jan-02-2024

You should post your code. I'm don't really understand what you are asking for. Something like this maybe?
def damage_calc(damage, prev_damage):
    return max(prev_damage / 2, min(prev_damage * 2, damage))

print(damage_calc(220, 100))
print(damage_calc(25, 100))
print(damage_calc(75, 100))
Output:
200 50.0 75
This code clips the damage value to be in the range prev_damage / 2 to prev_damage * 2.