views:

128

answers:

2
+3  Q: 

Spaceship objects

I'm trying to make a program which creates a spaceship and I'm using the status() method to display the ship's name and fuel values. However, it doesn't seem to be working. I think I may have messed something up with the status() method. I'm also trying to make it so that I can change the fuel values, but I don't want to create a new method to do so. I think I've taken a horrible wrong turn somewhere in there. Help please!

class Ship(object):

    def __init__(self, name="Enterprise", fuel=0):
        self.name=name
        self.fuel=fuel
        print "The spaceship", name, "has arrived!"

    def status():
        print "Name: ", self.name
        print "Fuel level: ", self.fuel
    status=staticmethod(status)

def main():

    ship1=Ship(raw_input("What would you like to name this ship?"))
    fuel_level=raw_input("How much fuel does this ship have?")
    if fuel_level<0:
        self.fuel=0
    else:
        self.fuel(fuel_level)

    ship2=Ship(raw_input("What would you like to name this ship?"))
    fuel_level2=raw_input("How much fuel does this ship have?")
    if fuel_level2<0:
        self.fuel=0
    else:
        self.fuel(fuel_level2)

    ship3=Ship(raw_input("What would you like to name this ship?"))
    fuel_level3=raw_input("How much fuel does this ship have?")
    if fuel_level3<0:
        self.fuel=0
    else:
        self.fuel(fuel_level3)

    Ship.status()

main()

raw_input("Press enter to exit.")
+4  A: 

In your status method self is not defined, because you made it a static method. It makes more sense to make it non-static, since every ship has its individual name. So just say

def status(self):
    print "Name: ", self.name
    print "Fuel level: ", self.fuel

and later call

ship1.status()
ship2.status()
ship3.status()
jellybean
+1. Also, Jam, make sure your indentation is correct. Both `__init__` and `status` must be indented in your class definition.
Fred Larson
Yes. I'd be interested to know why the OP thought it should be a static method.
Daniel Roseman
They are indented, it just didn't do it when I copied it here. Oh well. Haha, I'm not sure why I decided it should be static. I'm working through the Python book, and static methods were just covered, so I was attempting to make that work. But I suppose I should use it only when it makes sense!
Jam
+4  A: 

Each ship has its own fuel, so static isn't the way to go. If you don't want the parameter to look like a method, consider a property. It encapsulates the value-checking of the fuel as well.

class Ship(object):

    def __init__(self, name="Enterprise", fuel=0):
        self.name = name
        self._fuel = fuel
        print "The spaceship", name, "has arrived!"

    def status(self):
        print "Name: ", self.name
        print "Fuel level: ", self.fuel

    @property
    def fuel(self):
        return self._fuel

    @fuel.setter
    def fuel(self,level):
        if level < 0:
            self._fuel = 0
        else:
            self._fuel = level

In main(), consider loops to initialize the ships and display status instead of repeating code, and use ship.fuel instead of self.fuel. self is only valid in methods of the class.

def main():

    ships = []
    for n in range(4):
        ship = Ship(raw_input("What would you like to name this ship?"))
        ship.fuel = int(raw_input("How much fuel does this ship have?"))
        ships.append(ship)

    for ship in ships:
        ship.status()

main()
raw_input("Press enter to exit.")
Mark Tolonen