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 6570305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:44:25+00:00 2026-05-25T14:44:25+00:00

I’ve made two classes called House and Window. I then made a list containing

  • 0

I’ve made two classes called House and Window. I then made a list containing four Houses. Each instance of House has a list of Windows. I’m trying to iterate over the windows in each house and print it’s ID. However, I seem to get some odd results :S I’d greatly appreciate any help.

#!/usr/bin/env python

# Minimal house class
class House:
    ID = ""
    window_list = []

# Minimal window class
class Window:
    ID = ""

# List of houses
house_list = []

# Number of windows to build into each of the four houses
windows_per_house = [1, 3, 2, 1]

# Build the houses
for new_house in range(0, len(windows_per_house)):

    # Append the new house to the house list
    house_list.append(House())

    # Give the new house an ID
    house_list[new_house].ID = str(new_house)  

    # For each new house build some windows
    for new_window in range(0, windows_per_house[new_house]):

        # Append window to house's window list
        house_list[new_house].window_list.append(Window())

        # Give the window an ID
        house_list[new_house].window_list[new_window].ID = str(new_window)

#Iterate through the windows of each house, printing house and window IDs.
for house in house_list:
    print "House: " + house.ID

    for window in house.window_list:
        print "   Window: " + window.ID

####################
# Desired output:
#
# House: 0
#    Window: 0
# House: 1
#    Window: 0
#    Window: 1
#    Window: 2
# House: 2
#    Window: 0
#    Window: 1
# House: 3
#    Window: 0  
####################
  • 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-05-25T14:44:25+00:00Added an answer on May 25, 2026 at 2:44 pm

    Currently you are using class attributes instead of instance attributes. Try changing your class definitions to the following:

    class House:
        def __init__(self):
            self.ID = ""
            self.window_list = []
    
    class Window:
        def __init__(self):
            self.ID = ""
    

    The way your code is now all instances of House are sharing the same window_list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.