Str 对象不可调用(使用输入作为 class 中函数的参数)

Str object not callable (using input as arg for a function in a class)

我是一个非常新的编码员,第一次学习 python,致力于制作基于文本的冒险游戏作为自学项目。我已经设法(我认为)几乎可以正常工作 "engine",(感谢 Whosebug 和 Gamedev 的优秀人员),在我可以扩展游戏和添加之前只需要一些微调有趣的东西(我相信你们都会告诉我我错了,需要做很多改变,但这就是我来这里的原因。)

无论如何,我的代码在第 81 行碰壁了。

world = {}


def addToInventory(item):
    player.bag.append(item)


class Items:
    def __init__(self, name, info, weight, position):
        self.name = name
        self.position = position
        self.info = info
        self.weight = weight


class Weapon(Items):
    def __init__(self, name, info, damage, speed, weight, position):
        super().__init__(name, info, weight, position)
        self.damage = damage
        self.speed = speed


sword = Weapon("Sword", "A sharp looking sword. Good for fighting goblins!", 7, 5, 5, 0)
knife = Weapon("Knife", "A wicked looking knife, seems sharp!", 5, 7, 3, 5)
stick = Weapon("Stick", "You could probably hit someone with this stick if you needed to", 2, 3, 3, 2)
shackkey = Items("Shack Key", "A key! I wonder what it opens.", .01, 3)
cottagekey = Items("Cottage Key", "An ornate key with an engraving of a small cottage on one side", .01, 5)
Moonstone = Items("Moonstone", "A smooth white stone that seems to radiate soft white light", .05, 6)
flower = Items("Flower", "A beautiful wildflower", .001, 1)


class Room:

    def __init__(self, name, description, exits, actions, roominv):  # Runs every time a new room is created
        self.name = name
        self.description = description
        self.exits = exits
        self.actions = actions
        self.roominv = roominv


world['introd'] = Room('introd', "You are in a forest, you can hear wildlife all around you. Your sword lies at your feet. There seems to be a clearing in the distance.", {'N': "clearing"}, {"Search the ground", "Go North"}, [sword])

world['clearing'] = Room('clearing', "You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. To the South is the way you entered the forest. A well worn path goes to the East. In the distance a harp can be heard.", {'S': "introd", 'E': "forest path"}, {"Take flower", "Go south", "Go East"}, [flower])  # Flower is not coded as an item that can be added to inv. To do this, create an ITEM class and make WEAPON a subclass of ITEM, SHOULD ALSO MAKE "KEY" SUBCLASS
world['forest path'] = Room('forest path', "You begin walking down a well beaten path. The sounds of the forest surround you. Ahead you can see a fork in the road branching to the South and East. You can smell smoke coming from the South, and can hear a stream to the East", {'S': "cottage", 'E': "stream"}, {"Go South", "Go East"}, [stick])
world['stream'] = Room('stream', "You come upon a relaxing stream at the edge of the woods. It looks like there is something shiny in the water. To your South is a rickety looking shack, to your West is the forest path you came down", {'S': "shack", 'W': "forest path"}, {"Go South", "Go West"}, [shackkey])

world['shack'] = Room('shack', "In front of you is a shack, possibly used as an outpost for hunting. It looks dilapidated.", {'S': "inside shack", 'N': "stream"}, {"Go South", "Go North"}, None)


world['inside shack'] = Room('inside shack', "The inside of the shack is dirty. Bits of ragged fur are scattered about the floor and on a table against the back wall. A sharp looking knife is on the table. There is a key hanging on the wall by a string.", {'N': "shack"}, {"Go North", "Take Knife", "Take Key"}, [knife, cottagekey])

world['cottage'] = Room('cottage', "A quaint cottage sits in the middle of a small clearing, smoke drifting lazily from the chimney.", {'N': "forest path", 'S': "inside cottage"}, {"Go north", "Go South"}, [Moonstone])

问题在下面,在播放器 class 中。

class Player:
    def __init__(self, name, health, bag, room_name, move):
        self.name = name
        self.health = health
        self.bag = bag
        self.room = world[room_name]
        self.move = move


command = input('>>> ')
player = Player("Jeff", 100, [], 'introd', command)

正如您在下面看到的(希望如此),我正在尝试做的是 python 接受用户输入,检查他们的输入是否是可接受的方向,然后检查是否那个方向对应有一个出口,如果有,就把玩家的房间设置成那个出口对应的房间。

不幸的是,它不允许我使用我的命令属性作为 player.move 方法中的 "direction" 参数。

def move(self, direction):
    if direction not in self.Room.exits:
        print("You can't go that way!")
        return
    new_room_name = self.Room.exits['Room']
    print("moving to", new_room_name)
    self.room = world[new_room_name]


while True:

    if command in {'N', 'E', 'S', 'W'}:
        **player.move(command)**
    elif command == 'look':
        print(player.room.description)
        print('Exits', player.room.exits.keys())
    else:
        print('Invalid command')

Traceback (most recent call last):
  File "C:\Users\Daniel\Desktop\PythonPR\FlubbosRoomAndItem.py", line 81, in <module>
    player.move(command)
TypeError: 'str' object is not callable

我的问题源于对 Python 的普遍不熟悉。我仍在学习基础知识并将此项目用作一种 "crash course",同时也在学习 youtube 教程。这让我很烦恼,因为它可能有一个简单的解决方案,但我什至不知道从哪里开始看。搜索错误并没有帮助,因为它是在我收集到的许多不同情况下出现的。

无论如何,此代码中还有其他问题,但这些是我宁愿自己解决的问题,因此无需过分挑剔我,除非我需要修复某些严重错误。任何提示,Pseudo-code/just 告诉我 google 到底是什么,将不胜感激。谢谢

您的实例上有一个名为 move:

的属性
class Player:
    def __init__(self, name, health, bag, room_name, move):
        # ...
        self.move = move

这掩盖了方法。在找到 class 上的属性(包括方法)之前,实例的属性查找首先查看实例属性。

重命名该属性或方法。

travel 函数是在 Player class 之外定义的。它需要两个参数而不是一个。我假设您打算在 class 中定义它?缩进 travel 函数并将其移动到 Player class.