# Time sheet problem # Function to return a valid date def GetDate(Message): # Iterate until valid date entered in format dd/mm IsValid = False while not IsValid: InputDate = input(Message) # Length check if len(InputDate) == 5: # Format check if InputDate[2] == "/": # Type check if InputDate[0:2].isnumeric(): Day = int(InputDate[0:2]) if InputDate[-2:].isnumeric(): Month = int(InputDate[-2:]) # Range check - only checking month so not comprehensive if Month >= 1 and Month <= 12: IsValid = True MonthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] if not IsValid: print("Invalid date entered.") return MonthList[Month - 1], InputDate # Function to return valid number def GetNumber(Message): ValidInput = False # Validation while not ValidInput: Number = input(Message) # Type check if Number.isnumeric(): ValidInput = True if not ValidInput: print("Invalid number.") return Number # Function to return a month def GetMonth(Message): ValidInput = False # Validation while not ValidInput: Month = input(Message) # Lookup check if Month in ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]: ValidInput = True else: print("Invalid month.") return Month # Subroutine to update time sheet def UpdateTimeSheet(): Filename, InputDate = GetDate("Enter the date in the format dd/mm: ") Hours = GetNumber("Enter the number of hours: ") try: File = open(Filename,"a") File.write(InputDate + "\n") File.write(Hours + "\n") File.close() except: print("Failed saving the update.") # Subroutine to output the time sheet def OutputTimeSheet(): Filename = GetMonth("Input the month, e.g. January: ") try: File = open(Filename,"r") Data = File.read() File.close() print(Data) except: print("Error, file not found.") # Main program UpdateTimeSheet() OutputTimeSheet()