Programming in Pyython, I am trying to return some values as a list but I keep r
ID: 3672011 • Letter: P
Question
Programming in Pyython, I am trying to return some values as a list but I keep returning the address not the string value please help....here is the program I am working on.
print ("Hello World")
"""Declarations"""
"""Card info"""
class Cards:
def __init__(self,code):
self.code = code
if self.code == 111:
self.name = test11
self.attack = test12
self.defence = test13
if self.code == 112:
self.name = test21
self.attack = test22
self.defence = test23
def __str__toBoard(self):
return [self.name, self.attack, self.defence]
"def __str__toBoard(self):"
"report = str(self.name())"
"return report"
"Gameboard"
code = 111
Cards('code')
spot1 = Cards.toBoard()
print (spots1)
"""Turns"""
class startPhase:
def __init__(self, tap, count):
self.tap = 0
self.count = count + 1
def __init__(self, charge):
self.charge = charge + 1
self.draw
#def __init(self, draw):
#input draw
class mainPhase:
def __init__(self):
self.call()
self.bCall()
self.ability()
self.item()
self.spell()
#def __init__(self, call):
class attackPhase:
def __init__(self):
self.move()
self.action()
class finalPhase:
def __init__(self):
self.impact
class endPhase:
def __init__(self):
self.condition()
class Turns:
startPhase
mainPhase
attackPhase
finalPhase
endPhase
"Subphases"
"""Main Program"""
Explanation / Answer
Python has two different ways to convert an object to a string: str() and repr().
Printing an object uses str();
Printing a list containing an object uses str() for the list itself, but the implementation oflist.__str__() calls repr() for the individual items.
So you should also overwrite __repr__().
A simple
at the end of the class body will do the trick.
Example
class Person:
def __init__(self, id, name):
self.id = id
self.name = name
def __str__(self):
return str([self.id, self.name])
__repr__ = __str__
me = Person('123', 'John')
print(me)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.