6 Replies - 800 Views - Last Post: 18 October 2012 - 08:19 PM Rate Topic: -----

#1 landon2214  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 12

Creating a class

Posted 18 October 2012 - 06:45 PM

I've been assigned a program to write on classes. I'm not asking for the correct code. I just want a few pointers. I can't find a lot of references and have no idea if I'm writing the class correctly or not. All I know is that the main code won't run using the class I've made. Any help would be greatly appreciated. My main questions are: 1. I was given these instructions:
"Write a class named Banger. You will need to write the following methods for it:
__init__(): This method needs to accept the name of the driver (a string) and
the model of the car (also a string). Use the string “unknown” for
the default argument. It should also initialize the number of hits
that the car has received in the game to 0.
__repr__(): This method should return a string that consists of the name of the
driver, followed by a tab, and then the name of the model of the
car.
get_driver(): This method simply returns the name of the driver.
get_model(): This method simply returns the model of the car.
get_hits(): This method returns the number of times the car has been hit
during the course of the derby.
add_hit(): This method adds 1 to the total number of hits."

Class code:
class Banger():
    def __init__(self, driver, modelofcar):
       self.unknown = driver, modelofcar
       hits = 0

    def __repr__(self):
       driver = self.get_driver
       modelofcar = self.get_model
       return driver + '\t' + modelofcar

    def get_driver(self):
        driver = ""
        return driver

    def get_model(self):
        modelofcar = ""
        return modelofcar

Main code:
#from Banger import *
from random import randint
import time

#Registration from the file competitors.txt
cars = []

car_file = open("competitors.txt")

for each in car_file:
    car, type_car = each.split('\t')
    new_entry = Banger(car.strip(), type_car.strip())
    cars.append(new_entry)

print("There are now", len(cars), "cars in this here demolition derby.")
print("Registration is now closed.  Here's who we have in our race:\n")
for each in cars:
    print(each)

#Welcome
print("\nAnd now it's time for the race!\n")
print("Welcome ladies and gentlemen to the CSE 1384 Demolition Derby.")
print("We have cars in the arena to rock your world with all of the smashing")
print("and crashing that you could ever hope for.  It promises to be an")
print("exciting night.\n")

print("Our returning champion, " + cars[0].get_driver()+ ", has come tonight")
print("with his newest" + cars[0].get_model()+ ".")
print("The race hasn't started yet so he has " + str(cars[0].get_hits()) + "hits so far,")
print("but is he ever mobile.\nYee-haa!  Look at him move.  Yessiree, it should")
print("be a fine show.")


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a class

#2 landon2214  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 12

Re: Creating a class

Posted 18 October 2012 - 07:06 PM

I also removed the '#' in front of the import statement, so please ignore that mistake. But now I am getting an import error, and I have no idea why.
Was This Post Helpful? 0
  • +
  • -

#3 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Creating a class

Posted 18 October 2012 - 07:12 PM

Your main code, is it a separate class or just something you type in the Python interpreter?
Was This Post Helpful? 0
  • +
  • -

#4 landon2214  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 12

Re: Creating a class

Posted 18 October 2012 - 07:14 PM

View PostfromTheSprawl, on 18 October 2012 - 07:12 PM, said:

Your main code, is it a separate class or just something you type in the Python interpreter?

something just typed in
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Creating a class

Posted 18 October 2012 - 07:27 PM

What's the content of the competitors.txt? When you said it's not working does it mean no cars are returned? Your getters set the variables to empty when you're getting them.
Was This Post Helpful? 0
  • +
  • -

#6 landon2214  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 12

Re: Creating a class

Posted 18 October 2012 - 08:16 PM

View PostfromTheSprawl, on 18 October 2012 - 07:27 PM, said:

What's the content of the competitors.txt? When you said it's not working does it mean no cars are returned? Your getters set the variables to empty when you're getting them.


atraub's edit:
For those who don't want to download the text file:
Rarin Togo	sedan
Slugger Hard	station wagon
Rolin Over	volkswagon
Mini Van	station wagon
Max Military	jeep

Attached File(s)


This post has been edited by atraub: 18 October 2012 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Creating a class

Posted 18 October 2012 - 08:19 PM

I'm afraid I don't have the energy to look through this thoroughly tonight, but something does jump out at me immediately.

Quote

__init__(): This method needs to accept the name of the driver (a string) and
the model of the car (also a string). Use the string “unknown” for
the default argument. It should also initialize the number of hits
that the car has received in the game to 0.


class Banger():
    def __init__(self, driver, modelofcar):
       self.unknown = driver, modelofcar
       hits = 0


So, you were on the right track with the first two lines, but then you lost it. A default argument means, if the value is not provided, use this value instead. So, if a driver is not provided, the driver will be set to unknown. Also, hits needs to be part of "self" so that you can access it later.

here's an example of a default argument:
def age_in_dog_years(age=12):
    print("you are "+str(age*7)+" in dog years")


and if we ran this in the interpreter:
>>> age_in_dog_years(5)
you are 35 in dog years

>>> age_in_dog_years(7)
you are 49 in dog years

>>> age_in_dog_years()
you are 84 in dog years


Notice that in the third example, I didn't pass a value, so it defaulted to 12.

I've fixed this line to show you how it should look, but I put it in spoiler tags so you can choose not to look at it.
Spoiler

This post has been edited by atraub: 18 October 2012 - 08:19 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1