【问题标题】:How to properly use a function and it's syntax in python?如何在python中正确使用函数及其语法?
【发布时间】:2023-04-04 20:01:01
【问题描述】:

目前我正在开发一个基本的文本游戏,您可以选择与狼战斗的武器,从字典中驱动谁的健康,您可以选择的武器的统计数据也是如此。现在我想做的是设置它,这样我就不需要为整个伤害想法重复相同的代码代码,我想把它写在一个函数中,这样我就可以为每种武器类型回调函数并节省空间并减少代码。如果有人可以向我展示如何做到这一点,和/或对我的代码有任何建议,以及如何在不使用大量面向对象编程的情况下使其更短,那将意义重大。

感谢任何人提供帮助或只是阅读本文并抽出时间。

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!")

【问题讨论】:

  • 你的代码是 500 行。没有人会想要看透这一切。你确定不能发minimal reproducible example
  • 我把代码变小了,完全忘记了它有500行。
  • 在我看来这更适合代码审查 SE (codereview.stackexchange.com)。如果您对函数有基本的疑问,网上有很多很好的资源。
  • 好的,谢谢您的意见。这是我第一次写或使用类似的东西,所以我需要习惯它。

标签:
python
function