Jump to content

Fixing invalid synatx in my Python login window

Recommended Posts

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("\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
 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.")
 print("\nPassword incorrect. Please try again.")
 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
 username = input("\nEnter username: ")
 f = open("accounts.txt", 'r')
 info = f.read()
 if username in info:
 print("\nUsername unavailable. Please try again.")
 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")
 print("\nYour account has been created.")
 print("You can now log in.")
 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
 use_digits = True

 usePunctuation = input("\nWhat about symbols? [y]/n: ")
 if usePunctuation.lower() == "n":
 use_punctuation = False
 use_punctuation = True

 passwordLength = input("\nLength of the password [10]: ")
 if passwordLength == "":
 password_length = 10
 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")
 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...")
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.
 f = open("accounts.txt")
 for line in f:

#Program starts here


Edited by ScribhneoirIldanach
Link to comment
Share on other sites

  • 4 months later...

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':

    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')
            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()
                print('That is not a valid option')

            print(f'Your password is {newpass}')
            print('That username has already been taken')
            # Go back and ask for new username


Edited by menator01
correct typo
Link to comment
Share on other sites

  • Barand locked this topic
This topic is now closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.