samedi 2 janvier 2021

IF statement to run functions(subroutines)

I'm trying to get the code in python to first open and then display a specific record in a CSV-file (the code does this already).

Ignore the # marks, I'm waiting for others to complete their code in one place

I want it to then give a choice to run a function which will allow either the adjustment of a current booking (only allowing change of the 'end date'), or run the other function which allows for the change of a future booking (allowing both the start and end date).

Here's the code:

import csv,os


#find the customers Booking ID so a choice can be made(current or future)

def findrec(file):
    f=csv.reader(open(file,'r', newline='\n'))
    v=input("Enter Customer's Booking ID: ")
    for row in f:
        if v in row:
            for i in row:
                print (i,end='\n')


        
        
findrec("bookings.csv")

#asks for available date

#runsubroutine from menu 1A (NEEDS TO BE IMPORTED)
#checks if available

Y=input("Is the customer currently checked in Y/N? ")
if input == Y:
                def editrecordc(file_name):
                    f1=csv.reader(open(file_name,'r',newline='\n'))
                    f2=csv.writer(open("temp.csv",'w'))
                    rid= input("Confirm Booking ID to edit: ")
                    for row in f1:
                        if row[0]==rid:
                            v2=input("Enter new Booking End Date: ")
                            rec=(v2)
                            f2.writerow(rec)
                        else:
                            f2.writerow(row)
                editrecordc("bookings.csv")
                os.remove("bookings.csv")
                os.rename("temp.csv","bookings.csv")

else:
    def editrecordf(file_name):
        f1=csv.reader(open(file_name,'r',newline='\n'))
        f2=csv.writer(open("temp.csv",'w'))
        rid= input("Confirm Booking ID to edit: ")
        for row in f1:
            if row[0]==rid:
                v1=input("Enter new Booking Start Date: ")
                v2=input("Enter new Booking End Date: ")
                rec=(v1,v2)
                f2.writerow(rec)
            else:
                f2.writerow(row)        
    editrecordf("bookings.csv")
    os.remove("bookings.csv")
    os.rename("temp.csv","bookings.csv")

Aucun commentaire:

Enregistrer un commentaire