lundi 1 novembre 2021

How can I find the input variable iterating through a file with for loops and if statements?

I am trying to iterate through a list/file but somehow my if statement doesn't find the VehicleID2 variable even though it finds it when I use the find() function but not working when I am trying to do if l == VehicleID2. What should I change in the code? I know my coding is looking complicated but it is only part of the code which I am trying to change. Basically, I am trying to find a VehicleID2 variable with l loop in a file and then print the details only from that specific line (with VehicleID2 variable and the l+1 l+3...) in that file. I would be very glad if you guys answer me. I am a beginner and this is my first project so sorry for any "really bad coding stuff" but yeah :)))

Also, this code that I am showing is just part of my code. Requirements for options 1 2 and 4 (elif selection == "1" or "2" or "4" which are other parts of the code) were done correctly but I am stuck with this option 3 (the last screenshot explains the requirements of the option 3). So just please ignore the elif selection == "3": part.

My code;

    elif selection == "3":
    VehicleID2 = input("Please enter the vehicle ID you have rented: ")
    def RentComplete(VehicleID2):
        f1 = open("Vehicle.txt","r")
        a = f1.read()
        b = "\n"
        c = a.replace(b,",")
        d = c.split(",")
        def Transactions(carid):
            f4 = open("Transactions.txt","a+")
            f4.write(carid+","+h[i+1]+","+dateandtime+","+h[i+3]+","+endingodometer+"\n")
            f4.close()
        for i in range(len(d)):
            if d[i] == VehicleID2 and d[i+5] == "R":
                endingodometer = input("Please enter the current odometer reading: ")
                f1.close()
                d[i] = str(d[i])
                d[i+1] = str(d[i+1])
                d[i+2] = str(d[i+2])
                d[i+3] = str(d[i+3])
                d[i+4] = str(d[i+4])
                f2 = open("Vehicle.txt", "r+")
                f2.write(d[i]+","+d[i+1]+","+d[i+2]+","+d[i+3]+","+d[i+4]+",A")
                f2.close()
                from datetime import datetime
                now = datetime.now()
                dateandtime = now.strftime("%d/%m/%Y %H:%M:%S")
                f3 = open("rentVehicle.txt","r")
                e = f3.read()
                f = "\n"
                g = e.replace(f,",")
                h = g.split(",")
                for l in h:
                    if l == VehicleID2:
                        print("\nCar",l,"is returned from", l+1,"\n")
                        print("\t\t******************** Vehicle Details ********************\n")
                        print("Vehicle ID =",d[i],"\t\tDescription =",d[i+1],"\tDaily Rate =",d[i+4],"\n")
                        print("Renter ID =",l+1,"\tDate/time of return =",dateandtime,"\n")
                        print("Rent Starting Odometer =",l+3,"\tRent End Odometer =",endingodometer,"\tRental charges = Euros\n")
                        Transactions(d[i])
                        print(d[i],"is returned!")
                        f3.close()
                        MainMenu()
                        break
                    else:
                        print("nope")
                        f3.close()
                        break
                break
            else:
                f1.close()
                print("\nThe vehicle ID you entered does not exist. Please check the vehicle ID and try again.")
                MainMenu()
                break
    RentComplete(VehicleID2)  

Vehicle txt file;

XJY-604,Hyundai Getz,O,0,45.00,R
AJB-123,BMW mini,P,200,65.00,A
WYN-482,Ford Fiesta,O,0,40,A
BKV-943,Ford Ikon,P,150,60,A
JMB-535,Ford Flex,O,0,50,A
FKI-232,Fiat Egea,O,0,30,A
KLE-154,Toyota Corolla,O,0,40,A
ITO-444,Renault Clio,O,0,55,R
MAA-321,Honda Civic,O,0,70,A
IRK-948,Hyundai i20,O,0,30,A
TRY-475,Peugeot 2008,O,0,50,R
IBM-984,Skoda Superb,O,0,60,A
KRI-365,Opel Corsa,O,0,50,A
PMA-760,Citreon C3,O,0,55,A
NGT-407,KIA Sportage,O,0,60,A

rentVehicle txt file;

ITO-444,13123123,02/11/2021 00:29:49,123123
TRY-475,123123,02/11/2021 00:29:58,3123123
XJY-604,123123,02/11/2021 00:30:09,123123

Vehicle.txt file

rentVehicle.txt file

my code

Part of the project requirements

Aucun commentaire:

Enregistrer un commentaire