I will donate $10 via paypal whoever can help me with this code.

heres my assignment

Write a Python class named "Car" that has the following data attributes (please create your
own variable names for these attributes):

- Year_Model (for the car's model)
- Make (for the car's maker)
- Speed (for the car's current speed)

The class should have an __init__ method that accept its year, model, and make as arguments.
These values should be assigned to the object's corresponding attributes. Speed should be set
to 0 in __init__.

The class should also have the following methods:

- Accelerate: add 5 to the speed each time it is called
- Brake: subtract 5 from the speed each time it is called
- Get_Speed: return the current speed of the car

Write a program to create a Car object. Accelerate the car twice, print its speed, and brake it twice.

Upload all your programs to this drop box.

Im not really sure where to start for this

I'm not going to do your homework for you, but I've written a class that illustrates some general ideas for you. This shows you how to declare a class, with an __init__ method and another method. It illustrates how to pass parameters and assign values to class member variables. It shows how to instantiate an instance of a class that's been declared, and make use of it. Note that when the class is instantiated, the __init__ method is automatically called. Note that the "self" parameter (which can be called something else, if you want) is the first parameter in the list for a method, and it is required for every class method, and it represents the particular instance of the class that the code inside a class method is operating on.

# declare a class
class Bird:
    """
    This Bird class represents a Bird, it has a name, a color, a size,
    and an x,y,z position.  This is a doc string for the class.  It's
    always good to include doc strings for classes and class methods.
    """

    def __init__(self, name, color, size):
        """
        This __init__ method is called when an instance of a Bird is
        constructed.  Pass the name, color, and size.
        """
        self.name = name
        self.color = color
        self.size = size
        self.x = 0
        self.y = 0
        self.z = 0

    def flyToLocation(self, x, y, z):
        """
        Update the x,y,z position of the bird.
        """
        self.x = x
        self.y = y
        self.z = z

# If this module is being run directly, create an instance of a Bird, and test out its constructor and it's methods.
if __name__ == '__main__':
    fred = Bird('fred', 'red', 2.3)     # instantiate a red, 2.3 kg bird named 'fred', 
                                        # and assign it to a variable called fred.

    fred.flyToLocation(34, 29, 16)      # have fred fly to a location specified by x, y, z coordinates

    print "fred's info.."
    print "name: %s" % fred.name
    print "color: %s" % fred.color
    print "location: %d, %d, %d" % (fred.x, fred.y, fred.z)

Here's what happens when I run this program from a Linux command line, like this:

rmayes$ python bird.py
fred's info..
name: fred
color: red
location: 34, 29, 16

Here's what happens when I access 'help' on my class:

rmayes$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bird
>>> help(bird)

Help on module bird:

NAME
    bird

FILE
    /Users/rmayes/bird.py

CLASSES
    Bird
    
    class Bird
     |  This Bird class represents a Bird, it has a name, a color, a size,
     |  and an x,y,z position.  This is a doc string for the class.  It's
     |  always good to include doc strings for classes and class methods.
     |  
     |  Methods defined here:
     |  
     |  __init__(self, name, color, size)
     |      This __init__ method is called when an instance of a Bird is
     |      constructed.  Pass the name, color, and size.
     |  
     |  flyToLocation(self, x, y, z)
     |      Update the x,y,z position of the bird.

ok i got this so far but keep having a syntax error, still doesnt address my problem of adding, subtracting and getting current speed

# declare a class
class Car:
    """
    """

    def __init__(self, model, make, speed):
        """
        """
        self.model = model
        self.make = make
        self.speed = 0

    def methods(self, accelerate, brake, get_speed):
        """
        """
        self.accelerate = speed+5
        self.brake = speed-5
        self.get_speed = speed

if __model__ == '__main__':
    jeremy = Car('2009', 'ford ranger', 0)     #  
                                        
while self.speed == 0:
    accelerate, accelerate;
    print speed
if speed == 10:
    brake, brake;
    print speed
    
    print "Car Info: "
    print "model: %s" % jeremy.model
    print "make: %s" % jeremy.make
    print "speed: %s, %d, %d" % jeremy.speed

Why do you have a "methods" method? It seems very weird to me.

also it should be if __name__ == "__main__": also, you have a "self" reference outside the class, which will raise a NameError.

Keep working on your program.

Looks like you've made a good start, you've gotten some of the ideas correct.

Here are a few comments:

Looks like you've got the __init__ method right.

About the Methods method..

You've given the Car class a method called methods, which takes three parameters: accelerate, brake, and get_speed. I'm not sure where you're going with that. I think what you want to do is give the Car class 3 methods, one called Accelerate, another called Brake, and another called Get_speed.

When you say self.accelerate = speed + 5, you're not increasing the speed of the car. You're giving it an accelerate variable, and giving that variable a value of speed + 5. That's not what you want, you want to increase the existing value in the speed variable. You want self.speed = self.speed + 5. Does that make sense? This will increase the speed of the car by 5 every time you call that function. Which is kind of what "accelerate" implies.

Again, with brake, you've assigned a member variable, called brake to the car object. I'm not sure why you're doing that. I think what you want to do is decrease the value of the car's speed variable in a function called brake.

With get_speed, again, you've assigned a member variable, called get_speed, to the car object. What you want is a method which returns the car's speed. Here's an example of a method that returns the value of a member variable:

class Bird:
    def GetColor(self):
        return self.color

About the '__model__' line:

What's this __model__ thing? No, that's not correct. In my code, I have a line that says if __name__ == '__main__': Let me explain something about that. The file you've created on your disk that has this code in it, that's named with a .py extension, that file is called a python "module". Other python modules may be able to "import" that module and use the classes declared and such. There is a pseudo variable called __name__ that Python makes available, within a given module, with a value that is the name of that module. So for example if this file is called 'car.py', then, within that module, python gives the __name__ variable the value 'car'. If you're running the file directly, by saying python car.py at the command line, then python gives the __name__ variable the value '__main__'. Does that make sense?

So the reason I had that line in my code, and indented everything after it, is to make it so that the stuff in that if block will only be executed if the file is being executed directly, not if the file is being imported as a module. The reason to do that is so that other python files can import this one, and make use of the Car class, without executing the test code that tests out the Car class.


This code instantiates an instance of a Car object and assigns that instance to a variable called jeremy: jeremy = Car('2009', 'ford ranger', 0) That's great.


About the while self.speed == 0 line:

When you unindented your if line, above that, to the far left (which is correct), you stopped defining the 'methods' method of the car class, and stopped defining the Car class, and started creating code that will be executed when the module is called. 'self' is only defined within the method it was passed into. (Python passes that first argument in for us. I call it 'self', because that's the common convention. It is a reference to the particular Car object being operated on. In this case, the object which gets assigned to jeremy.)

What you want is while jeremy.speed == 0, not while self.speed == 0.

Also, the while line, and everything below it, should be indented to the right so that it is part of the if __name__ block, so that it won't get executed if the file is being imported by another module. This is all test code that is testing out the Car class:

if __name__ == '__main__':
..
while jeremy.speed == 0:
..
if jeremy.speed == 10:
..

Ok? Make sense?

Also accelerate, accelerate is not correct. To call a method of a class instance, you need to use a reference to the class instance first (like jeremy), then the name of the method, then parentheses, and pass in any arguments that that function takes. like this:

classInstance.methodName(argument1, argument2, ..)

In the case of accelerate, since you're not passing in any arguments (except 'self', which is passed for us by python) you would call jeremy's accelerate method like this:

jeremy.accelerate()

(But you have to define accelerate as a method of the Car class before doing this, of course).

Similarly, brake, brake is not correct. It should be jeremy.brake(), and brake should be a method of the car class. (Your 'methods' method does not define any methods other than 'methods'.)

Ok?

Hope that helps,

Shavais


p.s. Your last line also has a problem. The "%s, %d, %d" part asks it display the values of three variables, a string, a number, and another number; but you're only giving it a single variable, which is a number. The "%s, %d, %d" should just be "%d". Other than that, the print part looks fine. I notice that you're referencing members of the jeremy object there, which is correct.

commented: Very well explained. You might wanna use more [icode] tags though +1

i changed a few things and got this, but now keeps saying return function outside of program? this python is extremely difficult to me

# declare a class
class Car:
    """
    """

    def __init__(self, model, make, speed=0):
        """
        """
        self.model = model
        self.make = make
        self.speed = speed

    def accelerate(self):
        return self.speed + 5

    def brake(self):
        return self.speed - 5

    def get_speed(self):
        return self.speed
        
        """
        """

if __name__ == '__main__':
    jeremy = Car('2009', 'ford ranger', 0)       
                                        
while jeremy.speed == 0:
    return accelerate();
    print speed;
if jeremy.speed == 10:
    return brake();
    print speed
    
    print "Car Info: "
    print "model: %s" % jeremy.model
    print "make: %s" % jeremy.make
    print "speed: %d" % jeremy.speed

Remove the semicolons from your code, they are unnecessary.
You are using return incorrectly.
Instead of method(), you should call jeremy.method().
Also you should indent the last 10 lines of your program.
Accelerate method should actually change the speed property, not just return a value.

After you are done with this homework assignment, you should really learn more Python.
Fix the program, I've pointed out the main problems,
but you are pretty close so somebody (or I) might go ahead and finish the rest.

ok I got this, not sure if I fixed everything you addressed but when i run the program nothing appears, im not getting syntax errors no more but nothing prints or appears

# declare a class
class Car:
    """
    """

    def __init__(self, model, make, speed=0):
        """
        """
        self.model = model
        self.make = make
        self.speed = speed

    def accelerate(self):
        return self.speed + 5

    def brake(self):
        return self.speed - 5

    def get_speed(self):
        return self.speed
        
        """
        """

        if __name__ == '__main__':
            jeremy = Car('2009', 'ford ranger', 0)       
                                        
        if jeremy.speed == 0:
            print jeremy.accelerate(self)
    
        elif jeremy.speed == 10:
            print jeremy.brake(self)
    
            print "Car Info: "
            print "model: %s" % jeremy.model
            print "make: %s" % jeremy.make
            print "speed: %d" % jeremy.speed

You indented too many lines.
I'll just finish the rest of the code for you, the way I would write it, but you need more practice with Python.

# declare a class
class Car(object):
    """
    """

    def __init__(self, model, make, speed=0):
        """
        """
        self.model = model
        self.make = make
        self.speed = speed

    def accelerate(self):
        self.speed += 5

    def brake(self):
        self.speed -= 5

    def get_speed(self):
        return self.speed
        
        """
        """

if __name__ == '__main__':
    jeremy = Car('2009', 'ford ranger', 0)      
 
    #the assignment instructions were to
    #accelerate it twice, and break it twice
    #EDIT: I mean "brake" not break :)
    jeremy.accelerate()
    jeremy.accelerate()
    print jeremy.get_speed()

    jeremy.brake()
    jeremy.brake()
    print jeremy.get_speed()

thank you man, i added some modifications and its done, thank you very much

# declare a class
class Car(object):
    """
    """

    def __init__(self, model, make, speed=0):
        """
        """
        self.model = model
        self.make = make
        self.speed = speed

    def accelerate(self):
        self.speed += 5

    def brake(self):
        self.speed -= 5

    def get_speed(self):
        return self.speed
        
        """
        """

if __name__ == '__main__':
    jeremy = Car('2009', 'Ford Ranger', 0)
 
    jeremy.accelerate()
    jeremy.accelerate()
    print "---------------------------"
    print "The truck accelerates twice!"
    print "The trucks speed is now:"
    print jeremy.get_speed()
    print "mph"
    print ""

    jeremy.brake()
    jeremy.brake()
    print "---------------------------"
    print "The truck brakes twice!"
    print "The trucks speed is now:"
    print jeremy.get_speed()
    print "mph"
    print"----------------------------"
    print ""

#just below code was just added for fun
    """
    jeremy.accelerate()
    jeremy.accelerate()
    jeremy.accelerate()
    jeremy.accelerate()
    jeremy.brake()
    jeremy.brake()
    jeremy.accelerate()
    jeremy.accelerate()
    jeremy.brake()
    jeremy.accelerate()
    jeremy.accelerate()
    jeremy.brake()
    print "---------------------------"
    print "This is just a little extra bonus section"
    print "1. The truck accelerates four times"
    print "2. It then brakes twice"
    print "3. It accelerates two more times"
    print "4. It brakes once"
    print "5. It accelerates two more times"
    print "6. It brakes one final time"
    print "The trucks speed is now:"
    print jeremy.get_speed()
    print "mph"
    print"----------------------------"
    print ""
    """
    
print "Car Info: "
print "Model Year: %s" % jeremy.model
print "Make: %s" % jeremy.make
print "Speed: %d" % jeremy.speed

This is how I did it, if it helps at all.

class car:

    def __init__(self, model, make, speed):
        self.model = model
        self.make = make
        self.speed = speed

    def speedup(self, x):
        self.speed = self.speed + (5*x)
        
    def brake(self, x):
        self.speed = self.speed - (5*x)
    
    def carinfo(self):
        print ("Info..")
        print ("model: %s" % van.model)
        print ("make: %s" % van.make)
        print ("speed: %d" % van.speed)

if __name__ == '__main__':
    van = car('2001', 'Chrysler', 0)     

    van.speedup(2)
    van.carinfo()
    van.brake(2)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.