Python object at error

Hi! I’m trying to create a dice game in Python where two dice are rolled and the sum is displayed. Then, the user is asked if they want to play again. However, Python won’t print the dice. I keep getting an error that says " <main.die object at 0x0000012A26D2DA60>". What should I do? Thanks!
Here is my code:

import random print("Welcome to the dice game") output= random.choice([ 1,2,3,4,5,6]) class die: def __init__(self): self.die=output class DiceGame: def __init__(self,die1,die2): self.die= die1 self.other=die2 def __str__(self): if self.die==" ------\n| o o |\n| o o |\n| o o |\n ------ " or self.other== " ------\n| o o |\n| o o |\n| o o |\n ------ ": answer= answer+6 elif self.die==" ------\n| o |\n| o o |\n| o o |\n ------" or self.other== " ------\n| o |\n| o o |\n| o o |\n ------": answer=answer+5 elif self.die==(" ------\n| o |\n| o o |\n| o |\n ------")or self.other== (" ------\n| o |\n| o o |\n| o |\n ------"): answer=answer+4 elif self.die==" ------\n| o |\n| o |\n| o |\n ------" or self.other== " ------\n| o |\n| o |\n| o |\n ------": answer=answer+3 elif self.die== " ------\n| |\n| o o |\n| |\n ------" or self.other== " ------\n| |\n| o o |\n| |\n ------": answer= answer+2 else: answer= answer+1 return "Total: %s" % (answer) def __str__(self): return "%s \n %s" % (self.die,self.other) continue_playing= True while(continue_playing): response= input("Do you want to continue playing? Type 'Y' for yes or 'N' for no.") if response=="Y": die1= die() die2= die() show= DiceGame(die1,die2) print(show) if response== "N": continue_playing= False print("Thank you for playing!")

This doesn’t sound like an error. This is usually what happens when you try to print an object that has no __str__() method.
For whatever reason (I’m guessing you didn’t mean to) your DiceGame class has two string methods, meaning the last overwrites the first. Your last __str__() says to print two dice objects, and the die class has no __str__().
Which brings us back to what you are having printed. When an object has no __str__() defined, than the type of object and it’s memory location is printed.

1 Like

It looks like this is the only place you reference your text based die printout. If I’m interpreting this correctly, die1.die and die2.die are equal to random.choice([ 1,2,3,4,5,6]) and the first dicegame __str__ output is comparing the die class integers to strings. Before being overridden by the second __str__ method, the only correct answer was the else: answer= answer+1
One of your classes has to store and return the string representation of the die integer, or you can translate the integer for printing outside of the classes. There’s multiple approches you could take.