Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding issue
#3
You can use dict to store usernames and passwords instead of list, and save dict to a file and load it each time when you run the *program check my example bellow

import json
import os 


auth_file = "auth.json"
auth = None



def loadAuth():
    if os.path.isfile(auth_file):
        return eval(open(auth_file).read())
    return {}

def saveAuth():
    json.dump(auth, open(auth_file, 'wt'))


if __name__ == '__main__':
    auth = loadAuth()

    operation = input('Login in or new user: ')

    if operation == 'login':
        username = input('username: ')
        password = input('password: ')

        if username in auth.keys() and auth[username] == password:
            print('Access granted')
        else:
            print('Access Denied - Wrong username or password')

    elif operation == 'new user':
        username = input('New username: ')
        password = input('New password: ')
        password_conf = input('confirm password: ')

        if password == password_conf:
            if username not in auth.keys():
                auth[username] = password
                saveAuth()
                auth == loadAuth()
                print('Welcome ' + username)
                print (username,auth[username])
            else:
                print ("Username already exists")
Reply


Messages In This Thread
Coding issue - by 1557676 - Aug-02-2019, 06:46 PM
RE: Coding issue - by Yoriz - Aug-02-2019, 06:58 PM
RE: Coding issue - by cvsae - Aug-02-2019, 08:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Very basic coding issue aary 4 2,480 Jun-03-2020, 11:59 AM
Last Post: buran
  Very basic coding issue mstichler 3 2,600 Jun-03-2020, 04:35 AM
Last Post: mstichler
  Python Coding Issue! ankitdixit 3 92,060 Sep-25-2019, 06:31 AM
Last Post: rohanjoshi0894
  New member with simple coding issue shaikh 0 3,003 Apr-24-2017, 05:28 PM
Last Post: shaikh
  coding issue Lee 2 3,506 Feb-13-2017, 10:53 AM
Last Post: Lee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020