Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with a Player class for a player who can carry a limited number

ID: 3652422 • Letter: W

Question

Write a program with a Player class for a player who can carry a limited number of items, represented by strings. The class should define the following attributes:

name for the players name
max_items for the maximum number of items the player can carry at one time
items for the list of items that the player carries

The class should define the following methods:

inventory () should display the items that the player carries, or a message saying that the player has no items

take() should have a parameter to receive the item to be added to the player's set of items. The method should add the item to the player's items as long as adding it doesn't exceed the maximum number of items the player can carry; otherwise, the method should display a message saying that the player can't carry any more items.

drop() should have a parameter to receive the item to be removed from the player's set of items. If the item exists in the player's set of items, the method should remove it; otherwise, the method should display a message saying that the player doesn't carry that item

Your project should instantiate a Player object and call its take() and drop() methods to test the various outcomes

Explanation / Answer

#! /usr/bin/env python
#! coding: utf-8

class Player(object):
"""Player Object - hold player name and some items.
  
Details:
__init__(name): create player instance and pass name to it
all at once.
  
getName(): returns the name of the player instance.
  
....
....
write mor documentation here...
"""
  
def __init__(self, name):
"""Creates a player object and defining the following attributes:
self.name : holds the name of the players.
self.inventory: an empty list, holding the items, the player can pack"""
self.name = name
self.inventory = []
self.MAX_INVENTORY = 8
  
def getName(self):
"""Returns the players name"""
return self.name
  
def pack(self, item):
"""Packs an item into the inventory, as long
as the inventory's size does not exceed 8"""
if len(self.inventory) <= self.MAX_INVENTORY:
self.inventory.append(item)
  
def Inventory(self):
return self.inventory
  
  
if __name__ == "__main__":
player = Player("octopez")
print player.getName()
player.pack("Sword of Python-Power")
player.pack("A bit of python podcast")
inv = player.Inventory()
for item in inv:
print item

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote