我正在编写一个简单的基于文本的 RPG 游戏,并试图让库存系统正常工作。我已经逐字复制了工作代码,但我得到了一个语法错误。我不确定我做错了什么,因为它对我来说看起来不错。对不起新手问题。谢谢!
class Container(object):
def__init__(self, name):
self.name = name
self.inside = {}
def__iter__(self):
return iter(self.inside.items())
def__len__(self):
return len(self.inside)
def__contains__(self, item):
return item.raw in self.inside
def__getitem__(self, item):
return self.inside[item.raw]
def__setitem__(self, item, value):
self.inside[item.raw] = value
return self[item]
def add (self, item, quantity=1):
if quantity < 0:
raise ValueError("Negative quantity. Use remove() instead.")
if item in self:
self[item].quantity += quantity
self[item].recalc()
else:
self[item].quantity -= quantity
self[item].recalc()
运行时出现以下错误:
Traceback (most recent call last):
File "game.py", line 2, in <module>
from characters.player import *
File "/home/dave/Projects/rpg-master/characters/player.py", line 4, in <module>
from items.container import *
File "/home/dave/Projects/rpg-master/items/container.py", line 4
def__init__(self, name):
^
SyntaxError: invalid syntax
非常感谢您的帮助。