I think I have several questions before I post my code.

  1. How do I change the attributes? Everything looks the same and I was basically looking at one of the examples in my book for this problem. The book always has the arguments and attributes as being the same name. I hope that is clear enough.
  1. How do I get this thing to run? If it does run then I did something right.
  1. Why does it make another file with the ending ".Pyc" at the end?
  2. The file is black in color on a windows machine and silver on a Ubuntu machine.
  1. Last, how do I change the numbers so it reads 1,2,3 instead of all 1's
class Pet:

    def __init__(self,name,an_type,age):
        self.name =  name
        self.an_type = an_type
        self.age = age

    def set_name(self,name):
        self.name = name

    def set_an_type(self,an_type):
        self.an_type = an_type

    def set_age(self,age):
        self.age = age

    def get_name(self):
        return self.name

    def get_an_type(self):
        return self.an_type

    def get_age(self):
        return self.age
import awclassPet

def main():
    eman = raw_input("What is the name of your Pet?: ")

    atype = raw_input("What type of animal is it?: ")

    ega = input("What is the age of your animal?: ")

    animals = awclassPet.Pet(eman,atype,ega)

    print animals.get_name()
    print animals.get_an_type()
    print animals.get_age()

main()

Recommended Answers

All 5 Replies

Re: Why does it make another file with the ending ".Pyc" at the end?

When you saved your class module as awclassPet.py, then Python will also create a compiled bytecode version of this file the first time it is used by an application. The precompiled file awclassPet.pyc will speed things up on reuse. Also notice that module file names are case sensitive.

The variable names you create in main() are local to the function, so you can use the same variable names you use in your class.

You can test your module out with this arrangement:

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

    def set_name(self, name):
        self.name = name

    def set_an_type(self, an_type):
        self.an_type = an_type

    def set_age(self, age):
        self.age = age

    def get_name(self):
        return self.name

    def get_an_type(self):
        return self.an_type

    def get_age(self):
        return self.age


# allows you to test the module
# will not be used when the module is imported
if __name__ == '__main__':

    name = "Oscar"
    an_type = "cat"
    age = 13
    
    animals = Pet(name, an_type, age)

    print animals.get_name()
    print animals.get_an_type()
    print animals.get_age()
    print
    
    animals.set_name("Ruby")
    animals.set_an_type("cow")
    animals.set_age(17)
    
    print animals.get_name()
    print animals.get_an_type()
    print animals.get_age()

Tests okay!

I would suggest you write and run your code on an IDE made for Python code, something like IDLE or DrPython. Otherwise your output window will be set to the OS command window colors.

I would suggest you write and run your code on an IDE made for Python code, something like IDLE or DrPython. Otherwise your output window will be set to the OS command window colors.

And Wingware's Wing IDE if you need simple debugging

I wrote a class but I am clueless on how to implement it into a regular program. Any ideas? Supposed to make the car accelerate 5 times and brake 5 times. While its doing this, display the current speed.
Can it be explained without giving code if possible?

class Car(object):
    count = 0

    def __init__(self,y_model):

        self.__y_model = y_model
        self.__make = make
        self.__speed = 0


    def accelerate(self):
        
        if self.__speed <= 0 and self.__speed <=65:
            print "You are about to accelerate, put your seatbelt on"
            self.__speed +=5
            print self.__make, self.__y_model,"has accelerated to",self.__speed, "mph"
        else:
            print "Car is going to fast must brake"
            self.brake()
            
            

    def brake(self):
        if self.__speed >= -25 and self.speed <=65:
            print "I am braking hold tight"
            self.__speed -=5
    self.__get_speed

    def get_speed(self):
        return self.__speed

I wrote a class but I am clueless on how to implement it into a regular program. Any ideas? Supposed to make the car accelerate 5 times and brake 5 times. While its doing this, display the current speed.
Can it be explained without giving code if possible?

class Car(object):
    count = 0

    def __init__(self,y_model):

        self.__y_model = y_model
        self.__make = make
        self.__speed = 0


    def accelerate(self):
        
        if self.__speed <= 0 and self.__speed <=65:
            print "You are about to accelerate, put your seatbelt on"
            self.__speed +=5
            print self.__make, self.__y_model,"has accelerated to",self.__speed, "mph"
        else:
            print "Car is going to fast must brake"
            self.brake()
            
            

    def brake(self):
        if self.__speed >= -25 and self.speed <=65:
            print "I am braking hold tight"
            self.__speed -=5
    self.__get_speed

    def get_speed(self):
        return self.__speed

Here is what I came up with but I keep getting an __init__ error. Im going to try another way.

import awclasscar

def main():
    mod = raw_input('What year was your car made?: ')

    ekam = raw_input('What type of car are you driving?: ')

    start_speed = 0

    cars = awclasscar.Car(mod,ekam)
    cars.accelerate(start_speed)
    cars.brake(start_speed)
    print cars

main()

Here is simple example on class and methods. Get the whole taste from dedicated tutorial like here:
http://docs.python.org/tutorial/classes.html

# simple classexample
class Car():
    
    def accelerate(self, speed):
        acceleration = speed+1
        return acceleration
        
    def deccelerate(self, speed):
        deccel = speed-1
        return deccel

# now we will make use of class
#first make instance of that class
car_speed = Car()
speed = 5
print "The car started with speed %d m/s" %(speed,)
speed = car_speed.accelerate(speed)
print "Now car is moving at %d m/s" %(speed,)

speed = car_speed.deccelerate(speed)
print "Now car is moving at %d m/s" %(speed,)
commented: Thanks for the help +1
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.