So this is the code to a calculator I made for a game. Essentially what the calculator does is, it calculates the cost of buying the next stat.
So I wrote the code just for the Human race and the strength stat. From the way I see it, I will have to do for each race the same code 3 times for each stat.
I was hoping there would be a shorter way around this like
instead of human.strength
I would like it to be race.strength
where race = user_race
.
Thanks
class race:
"""The different races in the calculator"""
def __init__(self, race, strength, agility, health, strength_cost, agility_cost, health_cost):
self.race = race
self.strength = strength
self.agility = agility
self.health = health
self.strength_cost = strength_cost
self.agility_cost = agility_cost
self.health_cost = health_cost
human = race('Human', 15, 17, 18, 5, 3, 4)
elf = race('Elf', 11, 21, 14, 4, 3, 5)
giant = race('Giant', 25, 11, 27, 4, 8, 3)
print("Human, Giant, Elf")
user_race = str(input("Enter your race:")).lower()
print("Strength, Agility, Health")
user_stat = str(input("Enter your desired stat:")).lower()
user_present_stat_value = int(input("Enter your present stat value:"))
user_desired_stat_value = int(input("Enter your desired stat value:"))
if user_race == 'human' and user_stat == 'strength':
human_strength_present_statdif = (user_present_stat_value - human.strength) # difference of present stat with respect of base stat
human_strength_desired_statdif = (user_desired_stat_value - human.strength) #difference of desired stat with respect of base stat
human_strength_present_stat_cost = (human.strength_cost + (human_strength_present_statdif - 1) * human.strength_cost) #The cost of present stat stat
human_strength_total_present_cost = ((human_strength_present_statdif / 2) * (human.strength_cost + human_strength_present_stat_cost)) # The total cost from base stat to present stat
human_strength_desired_stat_cost = (human.strength_cost + (human_strength_desired_statdif - 1) * human.strength_cost) #The cost of desired stat
human_strength_total_desired_cost = ((human_strength_desired_statdif / 2) * (human.strength_cost + human_strength_desired_stat_cost)) # The total cost base stat to desired stat
human_strength_net_cost = (human_strength_total_desired_cost - human_strength_total_present_cost) # The Net cost from the difference of Total desired stat and Total present stat
print("Net cost: " + str(human_strength_net_cost))
```