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

  • SEARCH
  • Home
  • 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 8884675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:07:10+00:00 2026-06-14T21:07:10+00:00

I am working through the examples from Python Programming in Context my Miller &

  • 0

I am working through the examples from “Python Programming in Context” my Miller & Ranum.

I am working through the section on classes and we are building a solar system, and I am not clear on one certain part of the code.

class SolarSystem:

    def __init__(self, asun):
        self.thesun = asun
        self.planets = []

    def add_planet(self, aplanet):
        self.planets.append(aplanet)

    def show_planets(self):
        for aplanet in self.planets:
            print(aplanet)

    def num_planets(self):
        return len(self.planets)

I am not really sure how the add_planet and show_planets methods work exactly. In the shell I created a some planets using the “Planet” class. The planet class has more parameters than just name. It also includes name, radius, mass, distance from the sun and number of moons. When I pass a planet object to the SolarSystem class does the self.planets list contain all the arguments that were passed to the Planet object? If so how does the show_planet method know to print out the just names of planets? I think i may have missed something crucial here, one of the questions is to add a method for summing up the total mass in the solar system. But I am not sure how to access the instance variable for mass from the Planets class so I can sum them in the SolarSystem class.

I hosted a repository on github since there is more than one file and I didn’t want to paste it all here. solarsystem That way you can see what I am talking about.

I hope this makes sense, just ask if you’re unclear on anything.
Thanks

I added a total_mass method

def total_mass(self):
    total_mass = 0
    for aplanet in self.planets:
        total_mass = total_mass + get_mass(aplanet)
    return total_mass

It didn’t work and I am not sure why.

I got it!
The last line should be:

total_mass = total_mass + aplanet.get_mass()
  • 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-14T21:07:12+00:00Added an answer on June 14, 2026 at 9:07 pm

    self.planets contains all of the Planet objects, which do contain mass and such in addition to name. The reason it prints the planet’s name when you try to print the planet is that print, in order to print something, first needs to convert the object to a string. It can do this by passing the object to the str function. The str function converts it by calling __str__ on the object. If you look at the definition of Planet, __str__ returns the planet’s name, so when the planet is printed, its name is printed.

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

Sidebar

Related Questions

I am just working through some examples from my text book, writing the code
I am working through the example from https://blogs.oracle.com/arungupta/entry/java_ee_6_and_nosql . Here is a link to
I am working through some examples in a WCF book. There is a Host
I'm working through some MSDN examples, and some books on ADO.Net. What they all
I'm working through the Clojure examples in Stuart Halloway's Programmming Clojure and I've hit
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
I am working through the examples in a Django book that I have, but
Working my way through some some Python code, I'm noticing there are a number
I am working through the examples in Accelerated C++. One of the problems asks
I'm new to Python (and coding in general) and I'm working my way through

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.