I am doing a simple project for my first python course and I am stuck in one part which I have no idea how to continue.
So in this part user should input a vehicle id that he/she wants to rent. After putting the vehicle ID, my code starts to search for that vehicle ID in the Vehicle.txt text file. If the code finds the VehicleID variable and also finds that it is "A" (which means available), it starts printing details for that specific car.
My Vehicle.txt text file looks like this;
XJY-604,Hyundai Getz,O,0,45.00,A
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,A
MAA-321,Honda Civic,O,0,70,A
IRK-948,Hyundai i20,O,0,30,A
TRY-475,Peugeot 2008,O,0,50,A
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
So until this far, everything is fine; if the code finds the Vehicle ID (and the condition "A") then the code starts to print details as I want (if the condition is different or vehicle ID is not found it also prints appropriate error massages which are also perfectly working as I want).
My problem starts after this part:
After printing the details from the car, my code should change that specific car's condition from "A" (available) to "R" (rented) in the Vehicle.txt text file.
For example, let's say the user entered the Vehicle ID TRY-475 --> After this input, my excepted change in the Vehicle.txt text file is;
But the actual change in the text file is;
My code looks like this;
from datetime import datetime
now = datetime.now()
dateandtime = now.strftime("%d/%m/%Y %H:%M:%S")
def rentVehicle():
VehicleID = input("Please enter the vehicle ID you want to rent: ")
with open("Vehicle.txt","r+") as f1:
for line in f1:
l = line.split(",")
if l[0] == VehicleID and l[5] == "A\n" or l[5] == "A":
renterID = input("Please enter your ID: ")
startingodometer = input("Please enter the current odometer reading: ")
print("\nCar",l[0],"is rented to", renterID,"\n")
print("\t\t******************** Vehicle Details ********************\n")
print("Vehicle ID =",l[0],"\t\tDescription =",l[1],"\t\tDaily Rate =",l[4],"\tStatus =",l[5],"\n")
print("Renter ID =",renterID,"\tDate/time of rent =",dateandtime,"\tRent Starting Odometer =",startingodometer)
f1.write(l[5].replace(l[5],"R\n"))
print("\nRenting vehicle is successful!")
break
elif l[0] == VehicleID and l[5] == "R\n" or l[5] == "R":
print("\nThe vehicle you entered is rented. Please display available cars from the main menu.")
break
else:
print("\nThe vehicle ID you entered does not exist. Please enter a valid vehicle ID.")
break
rentVehicle()
I think the problem is in line 17 ( f1.write(l[5].replace(l[5],"R\n"))
). I searched for the other options but they also didn't give my excepted output in the Vehicle.txt text file. Additionally, I am not allowed to change my file name or add these lines to another file (manually or in the code) as it is restricted in my project work. I should only update the current Vehicle.txt via code. I would be very glad if someone solve this. For a beginner, these problems are really challenging sometimes. Thanks in advance.