Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:12:29+00:00 2026-06-13T14:12:29+00:00

After a few days of searching around and trying different code, I still am

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:12:30+00:00Added an answer on June 13, 2026 at 2:12 pm
    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"
            return self.user_text()  # don't forget to return in the recursion case
    
    def comp_door(self):
        choice = self.door_decision.user_text()  # actually use the returned result
    
        if choice == "left":
            self.left_door()
        elif choice == "right":
            self.right_door()
        else:
            print "incorrect input"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been searching for a few days trying to figure out how to turn
My code used to work, and after a few days of not touching it
After searching for a few days, i thought maby someone can help me. I'm
What I'm trying to do seems very simple, but after a few days of
I have been searching through here and google for a few days now, trying
after few days of freaking out and ugh frustrate thinking my API key is
After a few days of searching the net on how exactly I can go
I'm implementing RavenDB in a project and after a few days trying the database
I need to implement Android application which will be expired after few days or
I have been wrestling with this for a few days and after tons of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.