Python class if statement won't run

I’m trying to create a dice game where the user rolls two dice and the total prints. However, the if statement doesn’t seem to run properly and the total is always printed as zero. What should I do? Thanks!

import random class die: def __str__(self): options={6:" ------\n| o o |\n| o o |\n| o o |\n ------ ", 5: " ------\n| o |\n| o o |\n| o o |\n ------", 4:" ------\n| o |\n| o o |\n| o |\n ------", 3:"------\n| o |\n| o |\n| o |\n ------", 2:" ------\n| |\n| o o |\n| |\n ------", 1:" ------\n| |\n| o |\n| |\n ------"} self.value=random.randint(1,6) self.face=options[self.value] return self.face def __value__(self): return(self.value) class dieGame: def __init__(self, die1,die2): self.a= die1 self.b=die2 def __str__(self): answer=0 if self.a==" ------\n| o o |\n| o o |\n| o o |\n ------ " or self.b==" ------\n| o o |\n| o o |\n| o o |\n ------ ": answer= answer+6 elif self.a==" ------\n| o |\n| o o |\n| o o |\n ------" or self.b==" ------\n| o |\n| o o |\n| o o |\n ------": answer=answer+5 elif self.a==" ------\n| o |\n| o o |\n| o |\n ------" or self.b==" ------\n| o |\n| o o |\n| o |\n ------": answer=answer+4 elif self.a=="------\n| o |\n| o |\n| o |\n ------" or self.b=="------\n| o |\n| o |\n| o |\n ------": answer=answer+3 elif self.a== " ------\n| |\n| o |\n| |\n ------" or self.b== " ------\n| |\n| o |\n| |\n ------": answer=answer+1 elif self.a==" ------\n| |\n| o o |\n| |\n ------" or self.b==" ------\n| |\n| o o |\n| |\n ------": answer=answer+2 print(" %s \n %s" % (self.a,self.b)) return("Total: %s" % ( firstdie=die() seconddie=die() continue_playing= True while (continue_playing): response= input("Do you want to play the dice game? Type 'Y' for yes or 'N' for no.") if response=='Y': show= dieGame(firstdie,seconddie) print(show) else: continue_playing=False print("Thank you for playing!")

Is this different to your previous post? Python object at error

Have you tried using print or similar for a little debugging to see what the name refers to before it is returned? If you can find a spot that is not the value you expect then you can work backwards and trace the error.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.