For OOP learning reasons, (for now) created a dictionary class with regions and a class where you can move between regions with limited access, so for example from region 3 you are able only to go to regions 2, 4, 6, 8 or from region 10 only to region 9. Problem occured when I wanted to add description for regions. For few regions it reads wrong lines and can't find a reason.
(...)
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
for now is just:
test1
test2
(...)
test9
test10
Tried to move regions_file
and regions_description
to def movement
and use print(regions_description[region])
, but effect was the same.
CodePudding user response:
Couldn't solve it for few hrs earlier, but the idea came just when wanted to refresh my mind while jogging :) seems like a rookie mistake
In case someone else faces this problem in the future, adding "desc": regions_description[x]
to each row was a mistake, it should be only in rows of main keys.
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]}}