Home > Enterprise >  Code printing all contents of txt file instead of specific line
Code printing all contents of txt file instead of specific line

Time:04-28

I have the code written below and the goal of the program is to let the user list all cars in a list and then ask for specific details on just one specific car via input. However, when the user inputs the car they want details on, it also prints all of the cars in the txt file instead of just that one car.

The code from the main file where the program is written:

while True:
command = input("(L)ist all cars, get a cars (D)etails, or (Q)uit: ").strip().lower()

if command == "q":
    break
if command == "l":
    for car in cars:
        car.displayCar()
elif command == "d":
    carName = input("Enter car name: ").strip().lower()
    for car in cars:
        if car.is_match(carName):
            car.display()
else:
    print("Invalid command")

The code from the file where the above is using to call the users input:

    def is_match(self, carName):
    if self.carName == True:
        return False

    if carName == self.carName:
        return False

    return True

CodePudding user response:

def is_match(self, carName):
    if self.carName == True:
        return False

    if carName == self.carName:
        return True

    return False

You got your true and false mixed up in your is_match function. I bet if you really examine, you'll find that it's printing out every line except the line for the queried car.

CodePudding user response:

To answer the title of your question as I'm confused on what else you are asking:

with open(file,mode) as name:
   name.read() #Reads whole file
   name.close()

Now for is_match():

def is_match(self, carName):
    if self.carName == True:
        return True

    if carName == self.carName:
        return False

    return False
  • Related