I have been trying to find a way to make the below code work for a few days now. I have googled inheritance, class vs instance, etc and I still cannot find a solution… I’m trying to call an instance variable from one class (Starter_Cabin) to another class (Mystery_Forrest).
I’ve tried inheritance, super and simply calling the variable. I think that every time I try and call ‘self.weapon’ it simply re-loops through the if statement in the Starter_Cabin() - maybe the if statement needs to be rewritten?.. Any help is much appreciated.
from random import randint
from textwrap import dedent
from sys import exit
from operator import itemgetter
class Starter_Cabin(object):
def Enter(self):
print("You wake up and find yourself in a cabin... Pick a weapon.")
self.armory = [
"sword",
"axe",
"bow"
]
self.action = input("1. Sword\n2. Axe\n3. Bow\n\n> ")
if self.action == "1" or self.action.lower() == "sword":
self.weapon = itemgetter(0)(self.armory)
print("You reach for the", self.weapon, "and are amazed of how light it is.")
Mystery_Forrest().Enter()
elif self.action == "2" or self.action.lower () == "axe":
self.weapon = itemgetter(1)(self.armory)
print("You reach for the", self.weapon, "and find that it is more maneuverable than it looks.")
Mystery_Forrest().Enter()
elif self.action == "3" or self.action.lower() == "bow":
self.weapon = itemgetter(2)(self.armory)
print("You reach for the", self.weapon, "and find that it is as light as a feather.")
Mystery_Forrest().Enter()
else:
print("Please enter a valid option.\n")
Starter_Cabin().Enter()
class Mystery_Forrest(object):
def Enter(self):
SC = Starter_Cabin()
weapon = self.weapon
### The print line below is trying to call 'self.weapon' from Starter_cabin. I have also tried inheritance but the instance variable has to be initialised and when called reloops through the if statement above.
print("\nOnce you pick up your", SC.Enter(self.weapon), "you leave the cabin and find\nyourself in a forrest... This is no ordinary forrest.")
direction = [
"North",
"East",
"South",
"West"
]
action = input("Now that you can defend yourself, it's up to you\nwhere you want to go.\n1. North\n2. East\n3. South\n4. West\n> ")
if action == "1" or action.lower() == "north":
walk_way = itemgetter(0)(direction)
Mystery_Forrest_North().Enter()
elif action == "2" or action.lower() == "east":
walk_way = itemgetter(1)(direction)
Mystery_Forrest_East().Enter()
elif action == "3" or action.lower() == "south":
walk_way = itemgetter(2)(direction)
Mystery_Forest_South().Enter()
elif action == "4" or action.lower() == "west":
walk_way = itemgetter(3)(direction)
Mystery_Forest_West().Enter()
else:
print("Please enter a valid option.")
Mystery_Forrest().Enter()
class Mystery_Forrest_North(Mystery_Forrest):
pass
class Mystery_Forrest_East(Mystery_Forrest):
pass
class Mystery_Forrest_South(Mystery_Forrest):
pass
class Mystery_Forrest_West(Mystery_Forrest):
pass
Starter_Cabin().Enter()