Python Forum
Coding error- Not sure where I have put error markers against the code that is wrong
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding error- Not sure where I have put error markers against the code that is wrong
#1
#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()
Reply
#2
post the full traceback you get - in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Portfolio Optimization code error i want to cry FinanceMotta 3 406 Mar-11-2024, 10:24 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 475 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Error Code: 1 percyy30 13 1,926 Nov-07-2023, 01:36 PM
Last Post: percyy30
  Coding error. xflyerwdavis 2 520 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 696 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 2,033 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Wrong type error rowan_bradley 6 1,206 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,630 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,681 Jul-25-2023, 10:32 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020