Write a program with a Player class for a player who can carry a limited number
ID: 3558129 • 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
Define the Player class so a player object can be created with a unique name, maximum inventory size, and initial list of items in the player
Explanation / Answer
class Player:
totalItems=10
def__init__(self,name,items = []):
self.name=name
self.items=items
def inventory(self,items = [],name):
if items==0:
print('No items')
return 0
else:
return items[]
def take(self,item,maxitems,items = []):
if totalItems>maxitems:
items+=item
else:
print ('Limit exceeded')
def drop(self,item):
m=0
for item in items:
items=0
def main():
#create an object to player
play=Player.Player(name,items)
items=[]
for i in range(10):
var = raw_input("Please enter something: ")
items.append(var)
#call the main method
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.