python - 属性错误: 'int' object has no attribute 'directions'

标签 python

我已经更新了代码,使其看起来与我的计算机上的完全一样。

我一直在开发一个简单的 Python 文本冒险引擎,但遇到了一个问题。如果您可以解决我的问题或者有更有效的方法来编码/构造方法或类,请告诉我!我对这类事情很陌生,任何建议都会很棒!另外,格式可能达不到标准,但是嘿,我尝试过!

好的,我的项目有两个模块,标题为 GameMap。第一个模块,Game,“解析”用户输入并更改玩家所在的房间。第二个模块,Map,勾勒出一个Room > 包含构造函数和一些与方向、导出等相关的函数的类。我还没有解决移动到一个不存在的房间的问题,我确信我的代码相当糟糕,但这里是:

<小时/>

错误信息

>>> ================================ RESTART ================================
>>> 
You are in a small, cramped space.
It is cold and dark. There is a door to your North.

 > Go North

Traceback (most recent call last):
  File "C:\Users\Spencer\Documents\Python\Game.py", line 25, in <module>
    currentRoom = currentRoom.directions[0]
AttributeError: 'int' object has no attribute 'directions'
>>> 
<小时/>

游戏模块

# It is the main engine for PyrPG
import Map

currentRoom = Map.currentRoom

def updateGame():
    currentRoom.displayName()
def invalidCommand():
    print("Invalid command. Try again.")
while(True):
    updateGame()
    command = input(" > ")
    print()
    
    partition = command.partition("Go" or "go" or "Take" or "take")
    action = partition[1]
    item = partition[2]
    if(action == "Go" or "go"):
        if(item == " North" or " north"):
            currentRoom = currentRoom.directions[0]
        if(item == " East" or " east"):
            currentRoom = currentRoom.directions[0]
        if(item == " South" or " south"):
            currentRoom = currentRoom.directions[0]
        if(item == " West" or " west"):
            currentRoom = currentRoom.directions[0]
    else:
        print("Invalid command.")

        
<小时/>

map 模块

# It handles rooms.
class Room:
    rmCount = 0
    directions = [0, 0, 0, 0]
    
    def __init__(self, name, description):
        # Name and description
        self.name = name
        self.description = description

        # Directions
        self.directions = None
        self.canNorth = False
        self.canWest = False
        self.canSouth = False
        self.canEast = False

        # Increase room count
        Room.rmCount += 1
        
    def displayName(self):
        print(self.name)
        print(self.description)
        print()

    def displayDirections(self, directions):
        # Check directions, modify booleans
        if(self.directions[0] != 0):
            self.canNorth = True
            gNorth = "North"
        else:
            gNorth = ""
        if(self.directions[1] != 0):
            self.canEast = True
            gEast = "East"
        else:
            gEast = ""
        if(self.directions[2] != 0):
            self.canSouth = True
            gSouth = "South"
        else:
            gSouth = ""
        if(self.directions[3] != 0):
            self.canWest = True
            gWest = "West"
        else:
            gWest = ""

        print("Directions:", gNorth, gEast, gSouth, gWest)

    def setDirections(self, directions):
        self.directions = directions

    def displayInfo():
        displayName()
        displayDirections()

introRoom = Room("Welcome to the Cave.", "To start the game, type \"Go North\"")
storageCloset = Room("You are in a small, cramped space.", "It is cold and dark. There is a door to your North.")
mainOffice = Room("the main office.", "It is cold and empty.")
currentRoom = storageCloset

introRoom.setDirections([storageCloset, 0, 0, 0])
storageCloset.setDirections([mainOffice, 0, 0, 0])
mainOffice.setDirections([0, 0, storageCloset, 0])
<小时/>

再次强调,如果你看到什么可怕的东西,请告诉我!这是我在没有任何指导的情况下自己编写的第一个项目。谢谢!

最佳答案

问题出在这里...

if(item == " North" or " north"):

这是测试条件的错误方法。

你需要做的是

if (item == " North") or (item == " north"):

也请查看其他使用“或”检查条件的地方。

关于python - 属性错误: 'int' object has no attribute 'directions' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393965/

相关文章:

python - pandas 按分位数过滤结果为空集

Python正则表达式按数字在线分割

Python - ascii 编解码器无法解码位置 0 : ordinal not in range(128) 中的字节 0xe3

python - 我们如何获取 pyspark 中数据帧的每个分区的样本?

python - IO错误 : [Errno 2] No such file or directory writing in a file in home directory

用于框架的python selenium "Explicit Waits"

python - 使用 python 和 http 函数检查网络服务器中是否存在文件夹

python - 我该如何放置字典 {key : value} in it's designated key in a dictionary so that it is {key: {key: value}} after counting value

python - 从电子邮件中提取文本后,空格替换为 =20

Python Requests-Cache 仍在查询远程 URL