After a few days of searching around and trying different code, I still am not able to figure out my problem. Thus, I’ve posted this question here.
For this problem, I’m using Python 2.7.2
To be specific, I am using composition to import one class’s function into another class. The imported class’s function includes a simple if-statement based on raw_input. Depending on the user’s input, the if-statement somehow should call or at least help to call a new function corresponding to the input. This function, however will be in a part of the class that is importing, rather than the class that is imported.
I am using two .py files here, one for each of the classes, and they are in the same folder.
Here is the first file (main.py), which includes the main class:
# importing class from file in same folder
from class_decision import Decision
class Main_Compositor(object):
def __init__(self):
# using composition to call the function of the imported class
self.door_decision = Decision()
def comp_door(self):
self.door_decision.user_text()
if door == "left":
left_door()
elif door == "right":
right_door()
else:
print "incorrect input"
def left_door(self):
print "you're in the left room"
def right_door(self):
print "you're in the right room"
# instantiating
A_Compositor = Main_Compositor()
# calling A_Compositor's function comp_door()
A_Compositor.comp_door()
And here is the class_decision.py file, whose class is being imported:
class Decision(object):
def user_text(self):
print "which door do you open:"
print "left or right?"
door = raw_input("> ")
if door == "left":
print "you have chosen the left door"
return door
elif door == "right":
print "you have chosen the right door"
return door
else:
print "you must choose a door"
self.user_text()
As you can see I’m trying to use Return to let the main class know the variable door. This may be an incorrect use of Return. I’ve also tried playing around with getattr without success. I apologize if this question has been asked a lot. The similar questions to mine all seemed to do with arrays, and I couldn’t really figure out my problem through their answers. Thanks for the help.
1 Answer