Home > OS >  Login system (No GUI) giving wrong output
Login system (No GUI) giving wrong output

Time:09-29

I am trying to make a non-graphical login system.

I am expecting the Register() to take a username and password from the user, then from that open the file which stores the current data and see if the username exists already. From this it either creates the accounts and stores the info and if its taken it makes them re put in a new username. If I put in a name which already exists it will still create the account and store the info. What can I do?

Probably an easy fix with alot of bad code but I have been learning for under 2 week! Here is the code:

def start():
    global option
    option =  input('Welcome to Skies site!\nLogin or register?: ')
    if (option != 'login' and option != 'register'):
        print('Invalid input, try again.')
        start()
    elif option == 'login':
        Login()
    elif option == 'register':
        Register()

def Login(username, password):
    success = False # Set false so login isnt set to true at start
    file = open('user_details.txt','r') #R is read mode it will read the file
    for i in file:
        a,b = i.split(',') #Split on bases of comma to get the name and password
        b = b.strip() #Removes new line character (new empty line)
        if (a==username and b==password):
            success = True
            break
    file.close()
    if success == True:
        print('Login Successful!')
    else:
        print('Wrong username or password. Try again.')

def Register():
    global username
    username = input('Enter username: ')
    password = input('Enter password: ')
    with open('user_details.txt','a ') as file:
        if username in file.readlines():
            print('Username taken. Try another')
            Register()
        else:
            print('Username available!')
            file.write('\n' username ' , ' password)
            print('Credentials Saved!')
            print('--> Log in now <--')
            Login(username, password)

def user_info(username, password, verify):
    print('>>>>>>>>>>>>>>>>')
    print('Account is: ACTIVE')
    print('Username: ' username)

start()```

Thanks for in advance!

CodePudding user response:

You must use SQL database not text file for save informations!!

CodePudding user response:

Try this

def Register():
  global username
  username = input('Enter username: ')
  password = input('Enter password: ')
  with open('user_details.txt','r ') as file:
    if str(username) in str(file.readlines()):
      print('Username taken. Try another')
      Register()
    else:
        print('Username available!')
        file.write(username ' , ' password '\n')
        print('Credentials Saved!')
        print('--> Log in now <--')
        Login(username, password)

CodePudding user response:

  global username
  username = input('Enter username: ')
  password = input('Enter password: ')
  with open('user_details.txt','r ') as file:
    if str(username) in str(file.readlines()):
      print('Username taken. Try another')
      Register()
    else:
        print('Username available!')
        file.write(username ' , ' password '\n')
        print('Credentials Saved!')
        print('--> Log in now <--')
        Login(username, password)```
  • Related