从 txt 读取行或在 Python 3.10 中使用字典时出现问题

Problem with reading lines from txt or with dictionary usage in Python 3.10

出于 OOP 学习的原因,(目前)创建了一个包含区域的字典 class 和一个 class 您可以在访问受限的区域之间移动的字典,例如,您可以从区域 3只能去区域 2、4、6、8 或只能从区域 10 到区域 9。当我想为区域添加描述时出现问题。少数地区读错行,找不到原因。

(...)
class Territory(object):

    def __init__(self, **regions):
        player = Player()
        self.player = player
        regions_file = open("regiony.txt", "r")
        regions_description = regions_file.readlines()
        regions = { 
            1: {1: 2, "desc": regions_description[2]},
            2: {1: 3, "desc": regions_description[3],
                4: 1, "desc": regions_description[1]}, # prints regions_description[3] instead
            3: {1: 8, "desc": regions_description[8], # prints regions_description[4] instead
                2: 6, "desc": regions_description[6], # prints regions_description[6] instead
                3: 4, "desc": regions_description[4], # prints regions_description[8] instead
                4: 2, "desc": regions_description[2]},
            4: {2: 3, "desc": regions_description[3],
                4: 5, "desc": regions_description[5]},
            5: {1: 4, "desc": regions_description[4]}, # prints regions_description[6] instead
            6: {1: 7, "desc": regions_description[7],
                3: 3, "desc": regions_description[3]},
            7: {3: 8, "desc": regions_description[8], # prints regions_description[4] instead
                4: 6, "desc": regions_description[6]},
            8: {1: 9, "desc": regions_description[9],
                2: 7, "desc": regions_description[7],
                4: 3, "desc": regions_description[3]},
            9: {1: 10, "desc": regions_description[10],
                4: 8, "desc": regions_description[8]}, # prints regions_description[4] instead
            10: {4: 9, "desc": regions_description[9]}}
        self.__dict__.update(regions)
        regions_file.close()


class Moving(object):

    def __init__(self):
        self.territory = Territory()
        self.regions = self.territory.__dict__

    def movement(self):
        region = 1
        path = 1
        POSSIBLE_PATHS = (1, 2, 3, 4, 0)
        print(region)
        print(self.regions[region]["desc"])
        while path:
            try:
                path = int(input("Which path you take? "))
                if path in POSSIBLE_PATHS:       
                    region = self.regions[region][path]
                    print(region)
                    print(self.regions[region]["desc"])
                elif path == 0:
                        break
            except:
                while path not in POSSIBLE_PATHS: 
                    path = int(input("Which path you take? "))
(...)

regiony.txt 目前只是:

test1
test2
(...)
test9
test10

尝试将 regions_fileregions_description 移动到 def movement 并使用 print(regions_description[region]),但效果是一样的。

几个小时前无法解决它,但这个想法是在慢跑时想提神一下的时候产生的:) 似乎是一个菜鸟错误

以防以后有人遇到这个问题,每行加"desc": regions_description[x]是个错误,应该只在主键的行中。

            1: {1: 2, "desc": regions_description[1]},
            2: {1: 3, "desc": regions_description[2],
                4: 1},
            3: {1: 8, "desc": regions_description[3],
                2: 6,
                3: 4,
                4: 2},
            4: {2: 3, "desc": regions_description[4],
                4: 5},
            5: {1: 4, "desc": regions_description[5]},
            6: {1: 7, "desc": regions_description[6],
                3: 3},
            7: {3: 8, "desc": regions_description[7],
                4: 6},
            8: {1: 9, "desc": regions_description[8],
                2: 7,
                4: 3},
            9: {1: 10, "desc": regions_description[9],
                4: 8},
            10: {4: 9, "desc": regions_description[10]}}