首页 > 解决方案 > 如何重构此 Python 代码以使其更具可读性和紧凑性?

问题描述

我编写了一个函数来处理选择和使用项目以在文本冒险中恢复玩家的健康。缩小以下代码的最佳方法是什么?任何反馈或建设性的批评将不胜感激。

提前致谢。

另外,我真的很难理解面向对象的编程。有没有人推荐一本关于 OOP 主题的书(最好用 Python 例子来教)?

def use_item(action):
    print("What item do you want to use? Type the name of the item.")
    print("Inventory:")
    for items in player_inventory:
        print(items)
        action = input("> ")
        if action == "fruit":
            if action == "fruit" and "Fruit" not in player_inventory:
                print("You don't have any fruit.")
                print("Enter 'u' again if you want to select something else.")
            elif action == "fruit" and "Fruit" in player_inventory:
                print("You ate some fruit. You are refreshed.")
                player_inventory.remove("Fruit")
                player_health += 25
                if player_health > 100:
                    player_health = 100
                    print("You have",player_health,"health.")
                else:
                    print("Invalid choice! Try again.")
        elif action == "bread":
            if action == "bread" and "Bread" not in player_inventory:
                print("You don't have any bread.")
                print("Enter 'u' again if you want to select something else.")
            elif action == "bread" and "Bread" in player_inventory:
                print("You ate some bread. You are refreshed.")
                player_inventory.remove("Bread")
                player_health += 50
                if player_health > 100:
                    player_health = 100
                    print("You have",player_health,"health.")
                else:
                    print("Invalid choice! Try again.")
        elif action == "meat":
            if action == "meat" and "Meat" not in player_inventory:
                print("You don't have any meat.")
                print("Enter 'u' again if you want to select something else.")
            elif action == "meat" and "Meat" in player_inventory:
                print("You ate some meat. You are refreshed.")
                player_inventory.remove("Meat")
                player_health += 75
                if player_health > 100:
                    player_health = 100
                    print("You have",player_health,"health.")
                else:
                    print("Invalid choice! Try again.")
        elif action == "elixir":
            if action == "elixir" and "Elixir" not in player_inventory:
                print("You don't have any elixir.")
                print("Enter 'u' again if you want to select something else.")
            elif action == "elixir" and "elixir" in player_inventory:
                print("You ate drank an elixir. You are fully healed.")
                player_inventory.remove("Elixir")
                player_health += 100
                if player_health > 100:
                    player_health = 100
                    rint("You have",player_health,"health.")
                else:
                    print("Invalid choice! Try again.")
        else:
            print("Invalid choice! Try again.")   

标签: python-3.xrefactoringadventure

解决方案


您可以创建一个只有 if/elif/else 逻辑的函数,并且每个逻辑都有自己的函数。真的很好的解释 https://realpython.com/python3-object-orientated-programming/


推荐阅读