0

I'm working on a project and didn't do a good example of what I needed help with earlier so my thread was closed. What I need help doing is creating a class in Python that defines penny as an object, so later on I can go back in and change it to nickel, dime, quarter, etc, I have the basic idea of how to create it just want to make sure I'm on the right track or if I've completely vered off. In this I need to create the classes and then figure out how to add them by weight, and by height. In the beginning you will be asked to enter a number to go to a class and enter how many of those objects you have and add up the number.

This gives you an idea of what I've started to put together for the penny class that's all the data I need to have for the class just want to make sure I set it up correctly. Also how do I go about importing this into a new class like Quarter()

8
  • 1
    -1 for posting a screenshot instead of just cutting and pasting the code. Commented Mar 19, 2014 at 20:58
  • 1
    I thought the screen shot would be the easiest way to understand because Python is so sensative. sorry Commented Mar 19, 2014 at 20:59
  • why are you using a class for this exactly? this seems like its just data ... typically classes incorporate actions as well Commented Mar 19, 2014 at 21:00
  • 1
    I think you can only have a single init method in your python class. Commented Mar 19, 2014 at 21:01
  • 1
    @RobertMoskal you can have 2 ... only the last one will be used though ... Commented Mar 19, 2014 at 21:04

5 Answers 5

8

You probably want something like a Coin class for penny, nickel, dime, quarter to inherit from. Also you should only have one init method

class Coin():
    def __init__(self,weight,height):
        self.weight = weight
        self.height = height

class Penny(Coin):
    def __init__(self):
        Coin.__init__(self,2.5,1.52)
Sign up to request clarification or add additional context in comments.

2 Comments

you don't need class Foo():, In python2 use class Foo(object): or in python 3 just class Foo: (with no parens at all). It's not a problem, but looks sort of off.
you can do class Foo: in python2 also (at least in 2.6)
2

I would just use a dictionary for this since it only encapsulates data:

coin_dict = {1:(2.5,1.52),5:(nickle_weight,nickle_height),10:...}

weight,height = coin_dict[1]

Typically you would use a class if you wanted to encapsulate actions with data... or if your data gets very complicated.

Comments

1

Maybe you could have a parent class named Coin with a set of attributes, weight and height, depending on what you want to use it for. And then inherit from her to create a Penny or another coins.

class Coin:
     def __init__(self, height, weight):
         self.height = height
         self.weight = weight

class Penny(Coin):
      self.color = "bronze"

and go on from there. Then you can create a list and fill them with objects from the Penny, or Coin class.

Comments

0

Classes are a great way to represent real world data/objects in a digital format.

Before creating your class think about what you need it to do. You will want it to be reusable and flexible. In your case would creating a penny class be best or would a coin class be better? If you create a penny class you will have to create a nickel class and a dime class and so on. If you create a coin class you only have to instantiate a new one for each new type of coin.

A class can also only have one __init__ method. You can declare many attributes in on method though. For example:

class Pet:
    def __init__(self, name, species):
        self.name = name
        self.species = species

Comments

0

I would do something like this:

class Coin(weight, height):
    def __init__(self, weight, height):
        self.weight = weight
        self.height = height

If you want, you could add a method to, say, get the height and weight of the coin.

class Coin(weight, height):
    def __init__(self, weight, height):
        self.weight = weight
        self.height = height
    
    def get_properties(self):
        size = (self.weight, self.height)
        return size

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.