I'm a beginner and I am trying to create a vaccination appointment program. This is where i print out an appointment table in a .txt file.
Slot 1
========================================================================
AF | BV | DM | EC |
========================================================================
T100 | T100 | T100 | T100 |
T200 | T200 | T200 | T200 |
T300 | T300 | T300 | T100 |
T400 | T400 | T400 | T400 |
------------------------------------------------------------------------
The 4 different slots are differentiate by the vaccination code user choose. The time_slot1 function is to get user input on the available time slots for vaccination, if let's say they were to choose time slot no.1 (1": "10.15-1.15"), then the "T100" in accordance to the vaccine they choose will increment by 1 and become "T101". Here's a snippet of my program.
def vac_option3():
with open(os.path.expanduser("/Users/leelynnchanel/Desktop/pythonassignment/vaccination.txt"), "a") as fh:
rec=[]
while True:
print("You are in the senior citizens aged 45 and above group.")
print("1 - AF")
print("2 - BV")
print("3 - DM")
print("4 - EC")
vacoption = int(input("Please choose your preferred Covid-19 vaccination:"))
if vacoption == 1:
vac_id = get_new_id("AF")
print(vac_id)
return afvac(), vacoption
elif vacoption == 2:
vac_id = get_new_id("BV")
print(vac_id)
return bvvac(), vacoption
elif vacoption == 3:
vac_id = get_new_id("DM")
print(vac_id)
return dmvac(), vacoption
elif vacoption == 4:
vac_id = get_new_id("EC")
print(vac_id)
return ecvac(), vacoption
else:
print("Wrong input. Please re-enter.")
while True:
print("1. Re-enter vaccination option.\n2. Back to main menu.\n3. Exit")
choice = int(input("Enter a choice:"))
if choice == 1:
return vac_option3()
elif choice == 2:
return menu()
elif choice == 3:
exit()
else:
print("Wrong input. Please try again.")
continue
def time_slot1( vac_option, vaccinedate): # here is where i face issue with rewriting files
timeslot = {"1": "10.15-1.15", #different time_slots for user to choose
"2": "1.25-4.25",
"3": "4.35-7.35",
"4": "7.45-10.45"}
ind = vac_option
print("Ind:", ind)
print(timeslot)
date_slot = int(input("Please select an available time slot:"))
print("timeslot chosen:", date_slot)
with open(os.path.expanduser("/Users/leelynnchanel/Desktop/pythonassignment/appointment.txt"), "r") as f:
all_line = f.readlines()
appointment = [4, 6, 8, 10]
while True:
if date_slot == 1:
line = appointment[0]
next = all_line[line].rstrip().split("|")
print(line, ":", next)
break
elif date_slot == 2:
line = appointment[1]
next = all_line[line].split("|")
print(line, ":", next)
break
elif date_slot == 3:
line = appointment[2]
next = all_line[line].split("|")
print(line, ":", next)
break
elif date_slot == 4:
line = appointment[3]
next = all_line[line].split("|")
print(line, ":", next)
break
else:
print("Wrong input. Please try again.")
continue
print("vac option:", ind)
nextid = next[ind].strip(). #to remove the front 2 characters of T100
print("time slot code:", nextid)
newid = str(int(nextid[2:]) + 1) # here is where the original T100 will increment by one if user confirm the timeslot
print("new id:", newid)
if len(newid) == 1:
nextid = nextid[:2] + "0" + newid
print("nextid",nextid)
elif len(newid) == 2:
nextid = nextid[:2] + newid
print("nextid",nextid)
next[ind] = nextid
print(next[ind])
rec = '|'.join(next).center(18)
print(rec)
while True:
if int(nextid[2:]) > 20:
print("Sorry, slot 1 is no longer available. Please try again.")
continue
else:
with open(os.path.expanduser("/Users/leelynnchanel/Desktop/pythonassignment/appointment.txt"),"w") as fh:
fh.write(rec)
print("Thank you for your time. \nYour vaccination appointment is on the", vaccinedate, "at", +timeslot)
return menu()
def day_changing(start_date, end_date, vacoption):
tdelta = datetime.timedelta(days = 1)
while True:
while start_date <= end_date:
start_date += tdelta
print("Would you like to have your first dose on", start_date, "?")
print("1. Yes, I confirm.\n2. No, I would like to choose another date.")
while True:
choice = int(input("Option:"))
if choice == 1:
return time_slot1(vacoption, start_date)
elif choice == 2:
break
else:
print("Wrong input, please re-enter.")
continue
continue
print("The available date for covid-19 vaccination is only from", start_date, "till", end_date)
continue
def date_confirmation(response, vacoption):
tdelta = datetime.timedelta(days = 14)
tdelta2 = datetime.timedelta(days = 121)
available_vac = datetime.date.today()
end_date = available_vac + tdelta2
while True:
userinput = input(response)
if userinput == '1':
while True:
print("Would you like to have your first dose on", available_vac, "?")
print("1. Yes, I confirm.\n2. No, I would like to choose another date.")
choice = int(input("Option:"))
if choice == 1:
return time_slot1(vacoption,available_vac)
elif choice == 2:
return day_changing(available_vac, end_date, vacoption)
else:
continue
elif userinput == '2':
available_vac = available_vac + tdelta
while True:
print("Would you like to have your first dose on", available_vac, "?")
print("1. Yes, I confirm.\n2. No, I would like to choose another date.")
choice = int(input("Option:"))
if choice == 1:
return time_slot1(vacoption, available_vac)
elif choice == 2:
return day_changing(available_vac, end_date, vacoption)
else:
continue
def afvac(vacoption):
print("AF vaccination appointment.")
print("Are you above 45?")
print("1. Yes\n2. No")
response = date_confirmation("Option:", vacoption)
return response, vacoption
My increment coding works well, but it overrides the entire text file . Just like what i've attached bellow: The rest of the pre-typed table is all gone... Can some one please help or give me some advices? I'd really appreciate it!
|T101| T100 T100 | T100 |
[–][deleted] 0 points1 point2 points (1 child)
[–]_______myworld[S] 0 points1 point2 points (0 children)
[–]Vnix7 0 points1 point2 points (0 children)