I've just learned about using classes and I have this constant feeling that I'm just writing things inefficiently or wrong.
Could I get some advise on this quick "email" program? How would you have set this program up, more classes? methods outside of User class? Should I be using Global differently? Is my commenting structured well? I'm entirely self-taught so I want to know if I've developed any bad habits or something like that.
To get it to run, all you need is to replace the inboxLocation variable with a location on your station.
import os class User: # Domain name for emails and location for inboxes global emailDomain emailDomain = "@test.local" global inboxLocation inboxLocation = "E:/Users/Dylan/Desktop/Inboxes/" # User Creation def __init__(self, firstName, lastName, password): self.firstName = firstName self.lastName = lastName self.userName = firstName + lastName self.address = self.userName + emailDomain self.password = password self.inbox = inboxLocation + self.address # Create user inbox if it doesn't exist if not os.path.exists(self.inbox): os.mkdir(self.inbox) # Creates a header at the top of every page with the menu title and username def getHeader(self, currentMenu): header = (currentMenu + " | " + self.userName + "\n") print(header) # Display information about a user def getInfo(self): print("User Information for %s\n" % self.address) print("First Name: %s" % self.firstName) print("Last Name: %s" % self.lastName) print("Email Address: %s" % self.address) print("Inbox Location: %s\n" % self.inbox) input("\n[Enter to Continue...]\n") self.mainMenu() # Create and send an email to a user's inbox def sendEmail(self): self.getHeader("New Email") sender = self.address receiver = input("Sender Address: ") subject = input("Subject: ") body = input("Body:\n") # Create email file and store it in users inbox fileName = (inboxLocation + receiver + "/" + self.userName + " - " + subject + ".txt") emailFile = open(fileName, "w") emailContent = ("from: " + sender + "\n" + "to: " + receiver + "\n" + "subject: " + subject + "\n\n" + body) emailFile.write(emailContent) emailFile.close() # Confirm that email was sent if os.path.isfile(fileName): print("Email Sent! Returning to main menu.\n\n") self.mainMenu() else: print("Email Transmission failure, please try again.") self.sendEmail() # Look through a list of emails in inbox, delete function to be added def readEmails(self): self.getHeader("Inbox") # Iterate through inbox and assign numbers to each email for user selection print("Select an Email to read\n\nM) Main Menu") emailList = (os.listdir(self.inbox)) num = 0 for email in emailList: print(str(num) + ". " + email) num += 1 # Ensure user input is a both a number and within range readChoice = input("\n\n>>> ") try: # Exit to email if selected if readChoice == "M" or readChoice == "m": self.mainMenu() # Open selected email else: readChoice = int(readChoice) readEmail = open(self.inbox + "/" + emailList[readChoice], "r") print(readEmail.read()) readEmail.close() input("\n\n[Enter to Continue...]\n") self.readEmails() # Catch bad inputs except TypeError or IndexError: print("Please enter a valid number") self.readEmail() # Main menu method for moving in and out of menus def mainMenu(self): self.getHeader("Main Menu") choice = input("A) Send Email\nB) Inbox\nC) My Profile\nD) Logout\n\n>>> ") if choice == "a" or choice == 'A': self.sendEmail() elif choice == "b" or choice == 'B': self.readEmails() elif choice == "c" or choice == 'C': self.getInfo() elif choice == "d" or choice == 'D': self.logout() else: print("Invalid option, try again") self.mainMenu() # Logout method for changing users def logout(self): self.getHeader("logout") logoutChoice = input("are you sure you would like to log out?\n\n>>> ") if logoutChoice == "Yes" or logoutChoice == "yes" or logoutChoice == "Y" or logoutChoice == "y": login() # Login method called immediately after accounts are created def login(): print("Please enter your credentials\n") try: userEmail = input("Username: ") password = input("Password: ") # Raises a NameError if user is not found userEmail = eval(userEmail) if password == userEmail.password: print("Login successful, Welcome to your inbox", userEmail.address) userEmail.mainMenu() # Raise NameError if password is incorrect else: raise NameError # Handle NameError, inform user of mistake, restart login method except NameError: print("Username or password is incorrect\n\n") login() # Creates users on start of program, method to create users will be added. # Users will be stored in file with hashed passwords. def userCreation(): global jblow global bblow jblow = User("joe", "blow", "pass123") strudel = User("sylvester", "trudel", "P@ssw0rd") # Main method, initializes the program def main(): userCreation() login() # Run Main main()