Question
I have this template in Python, where the main function has to stay the way it is. After implementing a Character class and its methods
I have this "template" in Python, where the main function has to stay the way it is. After implementing a Character class and its methods correctly, running the given main should produce the following output. Note, that the same output has been expressed in two ways in the example below. The left column shows what the output actually looks like and the right column is there just to emphasize how the space characters should appear in the output. The program should only output what is shown on the left column.
The actual job is to implement the methods that main uses for the Character class. The implementation should be such that the given main's output with the class implementation looks like the example above.
Even though the constructor method __init__ only has one parameter, the items the character is carrying still need to be stored in some attribute.
Another important detail is that the printout method prints the items in an alphabetical order. In addition, if the character has no items, printout should display:
# Create a Character class class Character: # Default constructor def __init__(self, name): # Set the name of character self.name = name # Create an empty dictionary to store item name as key & count as value self.item = {} # Method used to get the name def get_name(self): return self.name # Method used to add the item in item dictionary def give_item(self, item): # If the item not is the item dictionary if item not in self.item: # Set the item name as key and number of item as 1 self.item[item] = 1 # If the item is in the item dictionary else: # Set the item name as key and number of item increased by 1 self.item[item] += 1 # Method used to remove an one item from item dictionary def remove_item(self, item): # If item is in the item dictionary if item in self.item: # Decrease the item count by 1 in item dictionary self.item[item] -= 1 # If item count is 0 if self.item[item] == 0: # Delete the item from item dictionary del self.item[item] # Method used to print the name of character & items with item count in sorted order def printout(self): # Print the character name print('Name:', self.name) # If the character has no item if not self.item: # Print "--nothing--" print(' --nothing--') # If the character has item else: # Create a list of names names = list(self.item.keys()) # Sort the names names.sort() # Create a sorted dictionary of item sorted_dict = {i: self.item[i] for i in names} # Loop for each name in dictionary for n in sorted_dict: # Print the item count and name of item print(' ', sorted_dict[n], n) # Method used to check item exist or not def has_item(self, item): # If item is in the item dictionary if item in self.item: # Return true return True # If item is not in the item dictionary else: # Return false return False # Method ised to return the number of items of particualr item def how_many(self, item): # Return the number of items of particualr item return self.item[item] def main(): # Create two objects of Character class character1 = Character("Conan the Barbarian") character2 = Character("Deadpool") # Assign items to character1 object for test_item in ["sword", "sausage", "plate armor", "sausage", "sausage"]: character1.give_item(test_item) # Assign items to character2 object for test_item in ["gun", "sword", "gun", "sword", "hero outfit"]: character2.give_item(test_item) # Remove the item "sausage" from character1 character1.remove_item("sausage") # Remove the item "hero outfit" from character2 character2.remove_item("hero outfit") # Call method printout() to print character1 & character2 object details character1.printout() character2.printout() # Loop for each character hero for hero in [character1, character2]: # Print character name print(f"{hero.get_name()}:") # Loop for each test item for test_item in ["sausage", "sword", "plate armor", "gun", "hero outfit"]: # If character hero has test item if hero.has_item(test_item): # Print the item name with number of items print(f" {test_item}: {hero.how_many(test_item)} found.") # If character hero has not test item else: # Print item name with none found print(f" {test_item}: none found.") # Call main() method if __name__ == "__main__": main()
but i get this error:
Name: Conan the Barbarian 1 plate armor 2 sausage 1 sword Name: Deadpool 2 gun 2 sword Conan the Barbarian: sausage: 2 found. sword: 1 found. plate armor: 1 found. gun: none found. hero outfit: none found. Deadpool: sausage: none found. sword: 2 found. plate armor: none found. gun: 2 found. hero outfit: none found. Name: Tanktop u Vegetarian us nothing Function call / Funktiokutsu: test_how_many() Printed something unexpected on the screen (or something was left out): Jotain odottamatonta tulostui nytlle (tai ji tulostumatta): 'gun' The expected output was: Odotettu tuloste oli: nothing/ei i mi tn The test function used looks like this: Kytetty testifunktio oli seuraava: def test_how_many(): chr = Character("Deadpool") chr.give_item("sword") chr.give_item("sword") chr.give_item( "mask") return [chr.how_many("sword"), chr.how_many("mask"), chr.how_many("gun")] Function call / Funktiokutsu: test_how_many() The test function used was: Kytetty testifunktio oli: def test_how_many(): chr= Character ("Deadpool") chr.give_item("sword") chr.give_item("sword") chr.give_item("mask") return [chr.how_many("sword"), chr.how_many("mask"), chr.how_many("gun")] returned: palautti: None The correct return value would have been: Oikea paluuarvo olisi ollut: [2,1,0]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started