# myApplication.py # page 64 # import MODULENAME module name is the filename without the extension # from MODULENAME import ObjectName # import MODULENAME as NameSpace/Alias # from myFunctions import fullName, cartTotal import myFunctions def header(): print("==== My Report Menus ====") print() # Create a list and loop thru using a for keyword to show options print("1. Report 1") print("2. Analysis 1") print("3. Set csv file") print("4. Report 2") print("5. Exit the report menu") print("========================") # Page 35 main() function def main(): try: # header and select menu header() ''' 3.10 page 155 optionSelected = int(input("Select option to run: ")) match optionSelected: case 1: myreport1() case 2: myreport2() ''' # while statement page 21 while True: # Ask the user what option to select optionSelected = input("Select option to run: ") # Call an if statement to run the selected option if optionSelected == "1": # Report 1 myFunctions.report1() elif optionSelected == "2": # Analysis 1 myFunctions.analysis1() elif optionSelected == "3": # write to csv file filename = input("Enter csv filename: ") # use for selecting csv filename # page 46 contacts = [ ["Bob", 21, True, 25.5 ], ["Mary", 32, False, 41.20], ["Sally", 31, True, 32.12] ] # Use a with open() to append each contact to a csv file ## mode a append -> adds a new object to the end of the file ## mode w write -> overwrite the complete file with new data ## mode r read with open(filename, mode="w", newline='') as file: import csv # Standard Lib in python, installed with python writer = csv.writer(file) csvHeader = ["ContactFirstName", "Age","Active","Points"] writer.writerow(csvHeader) for contact in contacts: writer.writerow(contact) print(f"Contact {contact} written to {filename} successfully.") elif optionSelected == "4": # Report 2 myFunctions.report2() elif optionSelected == "5": break else: print("You entered an invalid selection.") runAgain = input("Do you want to run another option? (yes/no) ").lower().strip() if runAgain != "yes": break except FileNotFoundError as e: print(e) except ZeroDivisionError as e: print("Your application tried to divide a number by zero. This causes an error.") except Exception as e: print(e) finally: print("Goodbye!") ### Actual Main Function Call Page 35 ### if __name__ == "__main__": main()