0
這裏的一些代碼,我在python了,我卡住有關如何在Python中的類打印字符串的方法...如果你可以... ...日Thnx很多請大家幫幫忙...如何在類中返回字符串方法?
代碼下面!
class Rat:
""" A rat caught in a maze. """
# Write your Rat methods here.
def __init__(Rat, symbol, row, col):
Rat.symbol = symbol
Rat.row = row
Rat.col = col
num_sprouts_eaten = 0
def set_location(Rat, row, col):
Rat.row = row
Rat.col = col
def eat_sprout(Rat):
num_sprouts_eaten += 1
def __str__(self):
""" (Contact) -> str
Return a string representation of this Rat.
"""
result = 'To: '
for contact in Rat.symbol:
result = result + '{0}, '.format(Rat.symbol)
result = result + '\nSubject: {0}'.format(Rat.row)
result = result + '\n{0}'.format(Rat.col)
return result
#return '{0} {1} <{2}>'.format(Rat.symbol,
# Rat.row, Rat.col)
我需要知道如何返回大鼠的字符串表示形式!
返回鼠標的字符串表示形式,格式如下: (行,列)處的符號用於num_sprouts_eaten芽菜。
例如:'J在(4,3)吃了2個豆芽。' 不要在字符串末尾放置換行符('\ n')。
那麼我將如何修復最後一種方法?
def __str__(self):
""" (Contact) -> str
Return a string representation of this Rat.
"""
result = 'To: '
for contact in Rat.symbol:
result = result + '{0}, '.format(Rat.symbol)
result = result + '\nSubject: {0}'.format(Rat.row)
result = result + '\n{0}'.format(Rat.col)
return result
#return '{0} {1} <{2}>'.format(Rat.symbol,
# Rat.row, Rat.col)
它需要打印出如下內容:'J在(4,3)吃了2個豆芽。'然而與上述代碼中,我得到一個錯誤時I型打印(對象)...我得到這個錯誤:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
print(a)
File "C:\Users\gijoe\Downloads\a2.py", line 61, in __str__
for contact in Rat.symbol:
AttributeError: class Rat has no attribute 'symbol'