Я написал функцию для выбора и использования предметов, чтобы восстановить здоровье игрока в текстовом приключении.Что было бы лучшим способом уменьшить следующий код?Любая обратная связь или конструктивная критика будет принята с благодарностью.
Заранее спасибо.
Кроме того, я действительно изо всех сил пытаюсь понять объектно-ориентированное программирование.У кого-нибудь есть рекомендации для книги по предмету ООП (желательно учить на примерах 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.")