Python Forum

Full Version: Coding error- Not sure where I have put error markers against the code that is wrong
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#from book_shop import BookShop
# THE ERRORS ARE MARKED NOT SURE HOW TO CORRECT THEM WITH THE CORRECT WORDING
# ITS CODE FOR A CAR HIRE AND RENTAL COMAPANY LOOKING TO RENT OUT CARS TO PEOPLE.
#Books to load from a file
filename="carstxt.rtf"

#Task1 Complete the Book class with attributes : title,author,publisher
class CarHireRent:
    def __init__ (self, type, model, rating, hire, rent):
        self.type=type
        self.model=model
        self.rating=rating
        self.rent=rent
        self.hire=hire
    def description(self):
        return "Type: {} Model: {} Rating: {} Hire: {} Rent: {}".format(self.type,self.model,self.rating, self.hire, self.rent)

class CarHireRent:
    
    def __init__(self):
        self.collection=[]


    #Task 2: load Cars from file    
    def load_cars(self):

        file = open("carstxt.rtf", "r")

        self.collection.clear()
        for line in file:    
        type, model, rating, hire, rent=line.split(",")  # split the line by commas: Type,Model,Rating,hire-rate,rental-rate.         
            self.add_cars(type, model, rating, hire, rent)

            file.close()
        

    #  Task 3 create a car object & add car object to collection
    def add_car(type,model,rating,hire,rent):
        car=Car(type, model, rating, hire, rent)
        self.collection.append(car)

        #Task 4:
        #  Display books in shop's collection
        def display_cars(self):
            for car in self.collection:
                print(car.description())

            #Task 5:
            # Search for cars by model and display the cars list
            # Display (No Books by author) if there are no books matching the author
    
            def search_by_model(self,name):
                count=0
                for cars in collection:
                    if (cars.model == name):
                        print(cars.description())    
                        count+=1
        
                    if (count ==0):
                        print("No cars by {name}")
            


        
#------------------------------------------------------------------------------
def main():
    
        print("---- Cars for Hire or Rent ----")
    
        shop=CarHireRent()
        shop.load_cars()  #load from file
    

        print("Cars list:")
        shop.display_cars()
   
         
        print("\nSearch cars by model")
        name=input("\tEnter model name:")
        shop.search_by_cars(name)
    
    
#Calling main        
main()
post the full traceback you get - in error tags