Python Str对象不可调用(将输入用作类中函数的参数)

Python Str对象不可调用(将输入用作类中函数的参数),python,string,dictionary,input,text-based,Python,String,Dictionary,Input,Text Based,我是一个非常新的程序员,第一次学习python,致力于制作一个基于文本的冒险游戏,作为一个自学项目。我已经设法(我认为)几乎让一个工作的“引擎”运转起来,(感谢stackoverflow和Gamedev的优秀员工),在我扩展游戏和添加有趣的东西之前,只需要一些微调(我相信你们都会告诉我我错了,需要做很多改变,但嘿,这就是我来这里的原因。) 不管怎样,我的代码在第81行碰到了一堵墙 world = {} def addToInventory(item): player.bag.appe

我是一个非常新的程序员,第一次学习python,致力于制作一个基于文本的冒险游戏,作为一个自学项目。我已经设法(我认为)几乎让一个工作的“引擎”运转起来,(感谢stackoverflow和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])
问题在下面,在Player类中

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方法中使用command属性作为“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
def移动(自身,方向):
如果方向不在self.Room.exits中:
打印(“你不能走那条路!”)
返回
新建房间名称=self.room.exits['room']
打印(“移动到”,新房间名称)
self.room=世界[新房间名称]
尽管如此:
如果{'N','E','S','W'中的命令:
**玩家移动(命令)**
elif命令=='look':
打印(播放器、房间、说明)
打印('Exits',player.room.Exits.keys())
其他:
打印('无效命令')
回溯(最近一次呼叫最后一次):
文件“C:\Users\Daniel\Desktop\PythonPR\FlubbosRoomAndItem.py”,第81行,在
玩家移动(命令)
TypeError:“str”对象不可调用
我这里的问题源于对Python的普遍不熟悉。我仍在学习基础知识,并将此项目作为一种“速成课程”,同时还学习youtube教程。这可能是一个简单的解决方案,但我甚至不知道从哪里开始寻找。搜索错误没有帮助,因为它是在许多不同的情况下出现的,与我收集的情况不同


不管怎么说,这段代码中还有其他问题,但这些问题我宁愿自己去解决,所以不必太挑剔我,除非我需要修复一些令人担忧的错误。任何提示,伪代码/只要告诉我谷歌到底是怎么回事,我都会非常感激。谢谢。

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

class Player:
    def __init__(self, name, health, bag, room_name, move):
        # ...
        self.move = move
这掩盖了这种方法。实例上的属性查找首先查看实例属性,然后再查找类上的属性(包括方法)


重命名该属性或方法。

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

class Player:
    def __init__(self, name, health, bag, room_name, move):
        # ...
        self.move = move
这掩盖了这种方法。实例上的属性查找首先查看实例属性,然后再查找类上的属性(包括方法)


重命名该属性或方法。

旅行功能是在您的
玩家
类之外定义的。它需要两个参数而不是一个。我想你想在课堂上定义它?缩进
travel
功能并将其移动到
Player
类下。

travel功能在
Player
类之外定义。它需要两个参数而不是一个。我想你想在课堂上定义它?缩进
travel
函数并将其移动到
Player
类下。

除了Martijn Pieters给出的正确答案外,行
new\u room\u name=self.room.exits['room']
可能会导致问题(如果我理解正确)。这可能是
new\u-room\u-name=self.room.exits[direction]
。除了Martijn Pieters给出的好答案之外,这行
new\u-room\u-name=self.room.exits['room']
可能会导致问题(如果我理解正确的话)。这可能是
new\u room\u name=self.room.exits[direction]
。嘿,我尝试了这个,但不幸的是它不起作用。我尝试分别重命名属性和函数,两者都导致了关于字符串不可调用的相同错误。如果没有正确的名称,则无法继续。确保您已经重新启动了解释器;如果重命名了方法和调用(因此
def do_-move(…):…
player.do_-move()
回溯没有改变你没有运行新代码。你好,Martjin,很抱歉这确实解决了我所问的错误,我在重新命名时犯了几个错误。@Schrodinger'sStat:对不起,你不能只是去把你的问题改成其他问题。请用你遇到的新问题发布一个新问题。这里的帖子旨在要对未来的访问者有所帮助,不仅仅是对你,而且我们希望问答帖子能够形成一个整体。通过改变你的问题,你只是把这个答案弄错了,不合适。我不得不把你的问题退回去,把另一个答案弄错了,不合适。你在这里对我和另一个回答者都造成了伤害。嘿,我尝试了这个不幸的问题y它不起作用。我尝试分别重命名属性和函数,两者都导致了关于字符串不可调用的相同错误。如果没有正确的名称,则无法继续。请确保已重新启动解释器;如果重命名了方法和调用(因此
def do_move(…):…
播放器。移动()
回溯没有改变你没有运行新代码。你好,Martjin,很抱歉这确实解决了我所问的错误,我在重新命名时犯了几个错误。@Schrodinger'sStat:对不起,你不能只是去把你的问题改成其他问题。请用你遇到的新问题发布一个新问题。这里的帖子旨在帮助未来的访问者,而不仅仅是你,我们希望问答文章形成一个整体