Future
Future

Reputation: 57

Even though I am using readlines(), these functions are only allowing me to read the first non-empty line but not the lines afterwards?

I dont really understand what the problem is. If the first non-empty line contains the license number I input then it prints out what I want, but as soon as I write the license number in a line afterwards, it wont print out anything. Maybe I am using readlines incorrectly?

def openfile(which_file): #FIXA DENNA!

    with open(which_file, "r") as file: #FELHANTERING OM FILEN INTE FINNS
         file_rows=file.readlines()
         passengers=[]
         for lines in file_rows:
             if lines !="\n":
                objekt=carclass.data_manager(lines.split()[0], lines.split()[1], lines.split()[2], lines.split()[9], lines.split()[13])
                passengers.append(objekt)
         return passengers



def history(passengers):

    which_car = input("Please write your license number: ")
    for p in passengers:
        if which_car==p.give_license_number():
           print(p.give_license_number(),p.give_size(),p.give_owner(),p.give_entry_time(), "-" ,p.give_exit_time())
           break
        else:
            print("Please write the license number of an existing car")
            return history(passengers)
class Size:

def __init__(self, small: int, medium: int, big: int):
    self.small= small
    self.medium= medium
    self.big= big

class data_manager:

def __init__(self, license_number, size, owner, entry_time, exit_time):
    self.license_number=license_number
    self.size=size
    self.owner=owner
    self.entry_time=entry_time
    self.exit_time=exit_time

def give_license_number(self):
    return self.license_number

def give_size(self):
    return self.size

def give_owner(self):
    return self.owner

def give_entry_time(self):
    return self.entry_time

def give_exit_time(self):
    return self.exit_time

Textfile:

PUT465 big Noel you entered the parking lot at 4:30 and left at 7:30

RCF585 big Joseph -you entered the parking lot at 4:30 and left at 6:45

If I Input PUT465, it prints out the line that contains it, but it dosent do that when I input RCF585

Upvotes: 0

Views: 41

Answers (1)

Joe Ferndz
Joe Ferndz

Reputation: 8508

In the function history(passengers), you are going through a loop.

The value of p first time is PUT465. So it will match the which_car. When you give a value of RCF585, the condition goes into else statement. You are calling the history(passengers) function again. It goes through the for loop again and starts all over. Instead, set a flag and if none of the values are in passengers, then route the process back to history(passengers)

def history(passengers):

    which_car = input("Please write your license number: ")
    for p in passengers:
        if which_car==p.give_license_number():
           print(p.give_license_number(),p.give_size(),p.give_owner(),p.give_entry_time(), "-" ,p.give_exit_time())
           break
        else:
            print("Please write the license number of an existing car")
        return history(passengers) #this line is making you go back to the function without checking for next value of passengers

Change your code as follows:

def history(passengers):

    which_car = input("Please write your license number: ")
    for p in passengers:
        if which_car==p.give_license_number():
           print(p.give_license_number(),p.give_size(),p.give_owner(),p.give_entry_time(), "-" ,p.give_exit_time())
           return #instead of break

    print("Please write the license number of an existing car")
    return history(passengers) #this line will execute only if for loop did not find a value of which_car

Note that return history(passengers) will go into infinite loop unless you give a valid license plate number.

Upvotes: 2

Related Questions