【发布时间】:2016-12-17 00:54:16
【问题描述】:
每次我尝试以当前状态运行此应用时都会收到此错误。
TypeError: cannot concatenate 'str' and 'GameData' objects
我正在尝试使用我的 html 从我的 Game(GameData) 类中获取一个数据对象到浏览器上。它是模板类 GameData() 的子类。
class GameData(object): #This class is the template for the data for each game.
def __init__(self):
self.title = ''
self.genre = ''
self.description = ''
self.developer = ''
self.rating = ''
self.image = ''
class Game(GameData): #Thas class holds all the data for each game.
def __init__(self):
#object for Castlevania
self.castlevania = GameData()
self.castlevania.title = 'Castlevania'
self.castlevania.genre = 'Action Platformer'
self.castlevania.description = 'Released in 1986 in Japan, Castlevania for the NES and Famicom Disc System spawned a series rich in action as well as exploration. This first game was merely a simple platformer but it inspired many changes to the formula over the years and invented the "Metroidvania" genre.'
self.castlevania.developer = 'Konami'
self.castlevania.rating = '7.5/10'
self.castlevania.image = 'images/t_castlevania.png'
还有其他对象,但如果我能让其中一个工作,我就能弄清楚其余的。我需要它来进入这个用注释突出显示的 elif 语句。
class MainHandler(webapp2.RequestHandler):
def get(self):
i = Intro()
d = GameData()
g = Game()
if self.request.GET:
page = self.request.GET['page']
if page == 'main_page':
self.response.write(i.head + i.main_page + i.main_title + i.main_links)
elif page == 'castlevania': #The data needs to go in this concatenation.
self.response.write(i.head + i.main_page + i.castlevania + (the data object should go here) + i.main_links)
从那里我知道该怎么做。我只需要知道如何将数据转换为字符串,以便将其连接起来。如果有人可以提供帮助,我将不胜感激。我也尝试对对象使用数组,但这对我也不起作用。
【问题讨论】:
标签: python string object concatenation