-1

The __repr__ function does not work in the following code:

class Minibar:
    def __init__(self, drinks, snacks):
        self.drinks=drinks
        self.snacks= snacks
        self.bill=0

    def __repr__(self):
        return "The minibar contains the drinks: " + list(str(self.drinks)) +  "And the snacks: "  + list(str(self.snacks)) +  "The bill for the minibar is:" + self.bill**

m = Minibar({'coke': 10, 'lemonade': 7}, {'bamba': 8, 'mars': 12})
print(m)
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
TxNull
  • 1
  • Could you give us the error code? – mime Jun 15 '20 at 13:44
  • You cannot concatenate lists and strings which is what you're appearing to do here. Your question is a little vague, so some extra detail would be helpful here. – hipeople321 Jun 15 '20 at 13:50
  • if you can also post error, that would be helpful – Anurag Wagh Jun 15 '20 at 14:28
  • The term "does not work" has no meaning. The next time please use a sentence that describes the problem like: "when I try to print this object it raises a `TypeError` saying `can only concatenate str (not "list") to str`". – Bakuriu Jun 15 '20 at 20:10

2 Answers2

0

You did not specify an error, I assume the interpreter does not let you concatenate a list with a string.

def __repr__(self):
    return "The minibar contains the drinks: " + str(self.drinks) +  "And the snacks: "  + str(self.snacks) +  "The bill for the minibar is:" + str(self.bill)
JoKing
  • 430
  • 3
  • 11
0

The python interpreter does not let you concatenate a list with a string. Instead, you can just convert the list to a string with str(list) or iterate through the elements in the lists:

class Minibar:

    def __init__(self, drinks, snacks):
        self.drinks = drinks
        self.snacks = snacks
        self.bill = 0

    def __repr__(self):
        string = "The minibar contains the drinks: "
        for drink in self.drinks:
            string += drink + " "

        string += "And the snacks: "
        for snack in self.snacks:
            string += snack + " "

        string += "The bill for the minibar is " + str(self.bill)

        return string
luke
  • 71
  • 9