如何重构此Python代码,使其更具可读性和紧凑性?

时间:2019-07-07 20:38:56

标签: python-3.x refactoring adventure

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

谢谢。

此外,我真的很难理解面向对象的编程。有没有人推荐一本有关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.")   

1 个答案:

答案 0 :(得分:0)

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