ScribhneoirIldanach Posted July 13, 2022 Share Posted July 13, 2022 (edited) The code below for a log-in window I've made always gets a "SyntaxError: invalid syntax" message whenever I try to run it in Python after I tried to fix a indentation error in the code. What am I doing wrong and why do I keep getting this error? #import packages import sys, time import string, random username = "" password = "" print("Welcome...") print("\nWould you like to:") #print the main menu print("\nA) Login") print("B) Create an account") print("V) View accounts") print("C) Quit") #enter the menu choice i = input("\nChoose [a/b/c]: ") if (i == "a" or i == "A"): #If the login is selected then it must check against the file #if username and password is found and display the messages accordingly print("\nLOGIN:") print("----------------------------------------------------------") username = input("\nEnter username: ") password = input("Enter password: ") f = open("accounts.txt", 'r') info = f.read() info = info.split() if username in info: index = info.index(username) + 1 usr_pass = info[index] if usr_pass == password: print("\nYou have successfully logged in.") time.sleep(1) sys.exit() else: print("\nPassword incorrect. Please try again.") else: print("No record of this user. Please register.") if (i == "b" or i == "B"): #New account creation with both the option #random password and manual password creation print("\nCREATING NEW ACCOUNT:") print("-------------------------------------------------------") username = input("\nEnter username: ") f = open("accounts.txt", 'r') info = f.read() if username in info: print("\nUsername unavailable. Please try again.") f.close() else: print("\nWould you like to:") print("\nA) Create your own password") print("B) Randomly generate a password") i = input("\nChoose [a/b]: ") if (i == "a" or i == "A"): password = input("\nEnter password: ") with open("accounts.txt", "a") as f: combo = username + " " + password f.write(combo + "\n") f.close() print("\nYour account has been created.") print("You can now log in.") else: print("\nLet's create a unique password for you...") useDigits = input("Would you like your password to include numbers? [y]/n: ") if useDigits.lower() == "n": use_digits = False else: use_digits = True usePunctuation = input("\nWhat about symbols? [y]/n: ") if usePunctuation.lower() == "n": use_punctuation = False else: use_punctuation = True passwordLength = input("\nLength of the password [10]: ") if passwordLength == "": password_length = 10 else: password_length = int(passwordLength) letters = string.ascii_letters digits = string.digits punctuation = string.punctuation symbols = letters if use_digits: symbols += digits if use_punctuation: symbols += punctuation password = "".join(random.choice(symbols) for i in range(password_length)) print("\nYour new generated password is: " + str(password)) with open("accounts.txt", "a") as f: combo = username + " " + password f.write(combo + "\n") f.close() print("\nYour account has been created.") print("You can now log in.") if (i == "c" or i == "C"): #option c exits the program after a delay of 2 seconds print("\nExiting program...") time.sleep(2) sys.exit() if (i == "v" or i == "V"): #open the file in read mode and read line by line and display on #screen. This will display all the account details from the file. print() f = open("accounts.txt") for line in f: print(line) f.close #Program starts here Edited July 13, 2022 by ScribhneoirIldanach Link to comment Share on other sites More sharing options...
requinix Posted July 13, 2022 Share Posted July 13, 2022 Despite starting with the same letter, PHP and Python are two different languages. The #1 rule of Python is to get your code indentation correct. Yours is very much incorrect. Try fixing that. Link to comment Share on other sites More sharing options...
menator01 Posted December 9, 2022 Share Posted December 9, 2022 (edited) I realize this is an old topic but, wanted to reply. This is in no way a complete working script but, could do something like this. I would suggest using a database such as sqlite3 or mysql for storing user data or maybe a json file as it would make it easier. from string import ascii_letters, digits from random import sample characters = ascii_letters + digits + '@!?&#' def generate(): return ''.join(sample(characters, 10)) def check_newuser(new_user): if new_user: return True return False def check(username, password): # Do user validation stuff if username: return True return False main_menu = ['A: Login','B: Create Account','C: Quit'] print('What would you like to do?') while True: print(f"Options: {', '.join(main_menu)}") option = input('Choose an option\n>> ') if option.lower() not in ['a','b','c']: print('That is not a valid option.') if option.lower() == 'c': print('Goodbye!') break if option.lower() == 'a': username = input('Please enter your username\n>> ') password = input('Please enter your password\n>> ') validate = check(username, password) if validate: print(f'User {username} logged in') else: print('Sorry that username or password is not correct.') if option.lower() == 'b': new_user = input('Please enter a username\n>> ') check_user = check_newuser(new_user) if check_user: print('Options: 1 create own password, 2 generate random password') new_password_option = input('Choose an option\n>> ') if new_password_option == '1': newpass = input('Enter a new password\n>> ') elif new_password_option == '2': newpass = generate() else: print('That is not a valid option') print(f'Your password is {newpass}') else: print('That username has already been taken') # Go back and ask for new username Edited December 9, 2022 by menator01 correct typo Link to comment Share on other sites More sharing options...
menator01 Posted December 9, 2022 Share Posted December 9, 2022 If there is interest in the thread I will write a full basic example using sqlite and a tkinter gui. Link to comment Share on other sites More sharing options...
Barand Posted December 9, 2022 Share Posted December 9, 2022 It isn't even your thread and, just in case you think we are prepared to host your personal Python blog, I'm closing it. Link to comment Share on other sites More sharing options...
Recommended Posts