i have to use the move method for the ship to move. the distance value is less than or equal to the objects fuel attribute.messgae should be displayed reporting the distance the ship moved and the objects fuel attribute should be reduced by the distance value. if not not enough fuel a message should say that it dont have enough fuel
refuel parameter to recieve the amount of fuel to add, if the amount great than or equal to 1 then should add fuel , and a message saying fuel has been added if less than one message should say ship cant be refueled.
in any case the last thing the method should do is call the object.

here what i got

class Ship(object):
    """A spaceship"""
    total = 0

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

    def move(self, fuel=1):
        print "ready to move."
        self.fuel = move
        if self.fuel <1:
            self.fuel = 1
            self._pass_time()

    def status():
        Ship.total += 1

        print "The total number of objects is", Ship.total
    status = staticmethod(status)

ship = Ship("enterprise")
print "\nCreating objects."
ship1 = Ship("object 1")
ship2 = Ship("object 2")
ship3 = Ship("object 3")
Ship.status()

print "\nship 1's fuel level is ", ship1.fuel

Assigning global values in objects attributes every time looks not ideal, like at line 13. You are using self.fuel, but fuel parameter is never used, and I do not understand parameter like that for move method. Fuel consumption per distance is not constant (inertia!)

move() should probably receive a distance parameter. You might use something like this, although it is not obvious from the question what the fuel and distance moved relationship is.

def move(self, distance):
        ## do you want to use floats or integers here?
        fuel_used = distance / miles_per_gallon   
        if self.fuel-fuel_used < 1:
            print "there is not enough fuel for that distance"
        else:
            self.fuel -= fuel_used
            print "You have traveled %d miles and have %d gallons of fuel" % \
                  (distance, self.fuel)

Edited 6 Years Ago by woooee: n/a

I would gladly help if I would understand what the move method is meant to do. I have read the post 3 times, but I do not get it.

Line 13 would raise NameError, if it would be called, because there is no "move" name...

Using the class to register its objects (as in Ship.total) requires some metaclass magic. I think, it is overkill.
Why not use a Ships class, define an add_ship method for that....?

im lost on the move method too. this is what i want to do add two method to the ship class:
move should have a parameterto recieve the distance for the ship to move. when it moves the distance value is less than or equal to the object fuel attribute, then the ship can move. a message should display saying the ship can move, and the ship fuel should be reduced based off the distance traveled. if the distance value is greater than the object fuel the method should display a message saying not enough fuel. the refuel method should gave a parameter to recieve the amount of fuel to add, if it amount add is greater than or equal to 1, then a message should say fuel has been added.
then should call each object status method.

i fixed someone of my code so it should be alittle better. and thanks for the help

#Behind the scenes

class Ship(object):
    """Creates a spaceship"""
    def __init__(self, name = "Enterprise", fuel = 0):
        self.name = name
        self.fuel = fuel
        if self.fuel < 0:
            self.fuel = 0

    def status(self):
        print "\nThe spaceship's name is", self.name
        print "\nAnd the ship's fuel level is", self.fuel

    def move(self, fuel=1):
        print "ready to move."
        self.fuel = move
        if self.fuel <1:
            self.fuel = 1
            self._pass_time()

    



#Main

ship1 = Ship()
ship2 = Ship("Firebird", 5)
ship3 = Ship("Flaming Justice", 10)
ship4 = Ship("Burning Desire", -4)

choice = None
while choice != "0":
    print \
    """

    -------------------------------
    SPACESHIPS

    0 - Quit
    1 - Check on Ship in dock 1
    2 - Check on Ship in dock 2
    3 - Check on Ship in dock 3
    4 - Check on Ship in dock 4
    """

    
    choice = raw_input("Choice: ")
    print

    if choice == "0":
        print "Good-Bye."
    elif choice == "1":
        ship1.status()
    elif choice == "2":
        ship2.status()
    elif choice == "3":
        ship3.status()
    elif choice == "4":
        ship4.status()
    else:
        print "This is not a valid choice"


raw_input("\n\nPress the enter key to exit.")

thansk for the time to look at my post i have since added some information to hopefuly help clear it up.

I would gladly help if I would understand what the move method is meant to do. I have read the post 3 times, but I do not get it.

Line 13 would raise NameError, if it would be called, because there is no "move" name...

Using the class to register its objects (as in Ship.total) requires some metaclass magic. I think, it is overkill.
Why not use a Ships class, define an add_ship method for that....?

yea i was thinking along the same way but i was never sure but i think i have made it somewhat more clearer on what i was wanting to do. thanks for helping me with my post.

move() should probably receive a distance parameter. You might use something like this, although it is not obvious from the question what the fuel and distance moved relationship is.

def move(self, distance):
        ## do you want to use floats or integers here?
        fuel_used = distance / miles_per_gallon   
        if self.fuel-fuel_used < 1:
            print "there is not enough fuel for that distance"
        else:
            self.fuel -= fuel_used
            print "You have traveled %d miles and have %d gallons of fuel" % \
                  (distance, self.fuel)

You still have to have a way to convert distance to fuel used. For that you have to know what the miles/kilometers per gallon/litre is. Note also that the question says that move() receives a distance parameter, not a fuel parameter as you have it, which implies that the function will convert distance to fuel used.

I cleaned up the main code, about move I do not know to help. Ship should have speed and time should go forward, space ship should have fuel consumption and movement vector (gravity too?), which must be reduced in each tick in time routing (and for space ship there is not speed per se, it is delta-v: cannot stop without fuel!)

#Behind the scenes

class Ship(object):
    """Creates a spaceship"""
    def __init__(self, name = "Enterprise", fuel = 0):
        self.name = name
        self.fuel = fuel
        if self.fuel < 0:
            self.fuel = 0

    def status(self):
        print "\nThe spaceship's name is", self.name
        print "\nAnd the ship's fuel level is", self.fuel

    def move(self, fuel=1):
        print "ready to move."
        self.fuel = move      # undefined global move variable
        if self.fuel <1:
            self.fuel = 1
            self._pass_time() # undefined method


#Main

ships = ('', Ship(), Ship("Firebird", 5), Ship("Flaming Justice", 10), Ship("Burning Desire", -4))

choice = None
while choice != "0":
    print """

    -------------------------------
    SPACESHIPS

    0 - Quit
    1 - Check on Ship in dock 1
    2 - Check on Ship in dock 2
    3 - Check on Ship in dock 3
    4 - Check on Ship in dock 4
    """

    
    choice = raw_input("Choice: ")
    print

    if choice == "0":
        print "Good-Bye."
    elif choice.isdigit() and 1 <= int(choice) <=4 :
        ships[int(choice)].status()
    else:
        print "This is not a valid choice"


raw_input("\n\nPress the enter key to exit.")

Edited 6 Years Ago by pyTony: n/a

I want to use miles per gallon when traveling. 1 mile travel uses 1 gallon of fuel. the only time i want to refuel() is when i try to travel a distance greater than what i have for fuel.

let see if what i said helps?

You still have to have a way to convert distance to fuel used. For that you have to know what the miles/kilometers per gallon/litre is. Note also that the question says that move() receives a distance parameter, not a fuel parameter as you have it, which implies that the function will convert distance to fuel used.

BEEP ... missing data ...

what is gravity (if it is zero, I can move any distance (as we have 1-d universe) by using almost zero fuel for acceleration and same to get back to 'stand still' orbit given enough time)

rate of acceleration/decceleration

distance travelled

current speed (distance traveled per tick)

current time in tics


As I understand it we are given travel in percentage of fuel capacity needed to accomplish travel, we will accelerate half way, turn around instantaniously, and decelerate half way to zero. Still the consumption / tick must be given somehow.

Edited 6 Years Ago by pyTony: n/a

heres all i got on what to do


add two methods to the ship class

move() should have a parameter to recieve the distance for the ship to move. if the distance value is less than or equal to the object's fuel attribute, the the ship has enough fuel to move. in that case, a message should be displayed reporting the distance the ship moved and the object's fuel attribute should be reduced by the distance value. if the distance value is greater than the objects fuel attribute, the method should display a message saying that the ship doesn't have enough fuel to move. if the distance value is less than 1, the method should display a message saying that the ship cant move a distance less than 1. in any case, the last thing the method should do is call the objects status() method.

refuel() should have a parameter to recieve the amount of fuel to add. if the amount to add is greater than or equal to 1, the value should be added to the objects fuel attribute and a message saying that the fuel has been added should be displayed. if the amount to add is less than 1, the method should display a message saying that the ship cant be refueled with an amount less than 1. in any case the last thing the method should do is call the objects status() method

your program should instantiate a ship object and call its move() and refuel() methods to test the various outcomes.

i posted all the information i have about my problem.

BEEP ... missing data ...

what is gravity (if it is zero, I can move any distance (as we have 1-d universe) by using almost zero fuel for acceleration and same to get back to 'stand still' orbit given enough time)

rate of acceleration/decceleration

distance travelled

current speed (distance traveled per tick)

current time in tics


As I understand it we are given travel in percentage of fuel capacity needed to accomplish travel, we will accelerate half way, turn around instantaniously, and decelerate half way to zero. Still the consumption / tick must be given somehow.

i have posted all the information i can on the problem.

You still have to have a way to convert distance to fuel used. For that you have to know what the miles/kilometers per gallon/litre is. Note also that the question says that move() receives a distance parameter, not a fuel parameter as you have it, which implies that the function will convert distance to fuel used.

This looks simple enough get/set pattern code from Java teacher, who has best idea to defeat him with plain attributes.

I think that there could be single method for those different things calling the status in the end and checking correct range, say

def _change_and_check(self, attribute, minimum, maximum)

Probably the object should also raise exception in case of wrong parameters to indicate place of malfunction in program, but that is disallowed by the task, which request not interupting execution, only message. (Small defect in O-ring, but let's continue counting...)

Alternatively the above pattern should maybe used as wrapper function for each parameter which need anti-Pythonic 'Type checking'. To protest this maybe we should allow any sequence of updates as parameter including generators.:twisted:

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.