I am pretty new to Python and programming in general, since I go to a programming school I am learning a lot of things by the book, which personally it doesn't suit me and I am falling behind everyone. Currently I am trying to change that and show that I am capable of doing something with my "knowledge" of Python, currently I am working on a basic text game, you can choose which weapon to fight a wolf, who's health is driven from a dictionary, same goes for statistics of the weapons you can choose from. Now what I was trying to do is set it up so I do not need to repeat the same code code for the whole damage idea and I wanted to write it down in a function, so I can call back the function for each weapon type and save on space and have less code. It would mean a lot if someone could show me how to do it, and / or any suggestions to my code and how I can make it shorter without using a lot of object oriented programming would mean the world.
Thank you for anyone helping or just reading this and taking the time of their day.
import time import random weapons = { "melee": { "katana": { "speed": 6, "strength": 50, "range": 5, "critical_chance": 50, "critical_multiplier": 2, "damage": 100 }, "dull_sword": { "speed": 3, "strength": 15, "range": 4, "critical_chance": 10, "critical_multiplier": 1.2, "damage": 30 }, "sharp_sword": { "speed": 3, "strength": 25, "range": 4, "critical_chance": 25, "critical_multiplier": 1.5, "damage": 45 }, "broken_machete": { "speed": 4, "strength": 20, "range": 3, "critical_chance": 15, "critical_multiplier": 1.3, "damage": 30 }, "repaired_machete": { "speed": 5, "strength": 30, "range": 3, "critical_chance": 20, "critical_multiplier": 1.4, "damage": 60 }, "mace": { "speed": 2.1, "strength": 60, "range": 4, "critical_chance": 10, "critical_multiplier": 1.1, "damage": 40 } }, "long_range": { "longbow": { "speed": 3, "strength": 55, "range": 100, "critical_chance": 100, "critical_multiplier": 1.5, "damage": 120 }, "slingshot": { "speed": 5, "strength": 10, "range": 50, "critical_chance": 25, "critical_multiplier": 1.1, "damage": 20 }, "short_bow": { "speed": 4, "strength": 15, "range": 60, "critical_chance": 20, "critical_multiplier": 1.2, "damage": 40 }, "broken_longbow": { "speed": 2, "strength": 30, "range": 70, "critical_chance": 50, "critical_multiplier": 1.3, "damage": 60 } }, "throwable": { "small_rock": { "speed": 7, "strength": 7, "range": 20, "critical_chance": 10, "critical_multiplier": 1, "damage": 10 }, "dynamite": { "speed": 2.1, "strength": 100, "range": 20, "critical_chance": 20, "critical_multiplier": 1.7, "damage": 120 }, "makeshift_explosive": { "speed": 2.3, "strength": 60, "range": 15, "critical_chance": 15, "critical_multiplier": 1.5, "damage": 95 } } } enemies_animals = { "dead": { "zombie": { "health": 150, "speed": 6, "damage": 20, "attack_speed": 4, "range": 3 }, "zombie_wolf": { "health": 250, "speed": 8, "damage": 25, "attack_speed": 4, "range": 3 }, "zombie_bear": { "health": 350, "speed": 4, "damage": 35, "attack_speed": 4, "range": 4 } }, "animals": { "chicken": { "health": 20, "speed": 6 }, "wolf": { "health": 100, "speed": 12, "damage": 20, "attack_speed": 3, "range": 3 }, "bear": { "health": 200, "speed": 6, "damage": 25, "attack_speed": 3, "range": 4 }, "cow": { "health": 50, "speed": 6 }, "large_bat": { "health": 100, "speed": 14, "damage": 15, "attack_speed": 5, "range": 2 } }, "regular_enemies": { "bandit": { "health": 100, "speed": 10, "damage": 10, "attack_speed": 5, "range": 3 }, "hoarder": { "health": 120, "speed": 10, "damage": 20, "attack_speed": 4, "range": 3 }, "mage": { "health": 90, "speed": 10, "damage": 25, "attack_speed": 3.3, "range": 15 }, "marksman": { "health": 75, "speed": 12, "damage": 50, "attack_speed": 2.2, "range": 50 }, "heavy_bandit": { "health": 150, "speed": 7, "damage": 15, "attack_speed": 3.4, "range": 4 }, "heavy_hoarder": { "health": 170, "speed": 7, "damage": 20, "attack_speed": 3.7, "range": 4 } } } wolf = enemies_animals["animals"]["wolf"]["health"] user = input("Write down your username: ") time.sleep(0.5) userInput = input("Welcome, for this test please choose either to attack, or to run: ") time.sleep(0.5) if userInput.lower() == "attack": time.sleep(0.5) weapon_type = input("Choose which type of weapon to use (melee/long_range/throwable): ") if weapon_type.lower() == "melee": time.sleep(0.5) weapon_list = [] for key in weapons[weapon_type]: weapon_list.append(key) print(f'Choose from weapons: {weapon_list}') time.sleep(0.5) weapon = input("choose which weapon to use(seen from the list above): ") critical_chance = weapons[weapon_type][weapon]["critical_chance"] if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"] attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") else: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3 attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") elif weapon_type == "long_range": time.sleep(0.5) weapon_list = [] for key in weapons[weapon_type]: weapon_list.append(key) print(f'Choose from weapons: {weapon_list}') time.sleep(0.5) weapon = input("choose which weapon to use(seen from the list above): ") critical_chance = weapons[weapon_type][weapon]["critical_chance"] if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon][ "strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"] attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") else: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3 attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") elif weapon_type == "throwable": time.sleep(0.5) weapon_list = [] for key in weapons[weapon_type]: weapon_list.append(key) print(f'Choose from weapons: {weapon_list}') time.sleep(0.5) weapon = input("choose which weapon to use(seen from the list above): ") critical_chance = weapons[weapon_type][weapon]["critical_chance"] if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon][ "strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"] attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") else: total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3 attack = wolf - total_damage if attack <= 0: dead_wolf = wolf if random.randint(1, 5) == 5: time.sleep(0.5) print(f"{user} killed a wolf with a critical hit and got it's meat! ") else: time.sleep(0.5) print(f"{user} killed the wolf with a critical hit!") else: time.sleep(0.5) print(f"The wolf has {attack} health! ") else: time.sleep(0.5) print(f"{user}, choose to run away!")