Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic log-in system
#1
i am needing some help on a few things:
1 - how to load new data into a text file for a new user
2 - I need to have a function called change password and I cant get the code to work to change a value in a dictionary/text file.

The following is my code:

def main():
    ok, name = checkLogin()
    if ok:
        print("Welcome", name.upper())
        runMenu()
    else:
        print("OOPs, failed to login...")
    print("BYE!!!")
#end def-----------------

def checkLogin():
    global users, uName, pWord
    users = {"admin":{'pwd':"admin",'valid':"VALID", 'fName':"root"}, "fred":{'pwd':"fred", 'valid':"VALID", 'fName':"Fred", 'sName':"Smith"}}

    with open("UserDetails.txt", 'w') as dFile:
        for k, v in users.items():
            dFile.write(k + " | ")
            for innerKey, detail in v.items():
                dFile.write(innerKey + " | " + detail + " | ")
            dFile.write("\n")

    validUser = False
    numAttempts = 0
    while (not validUser) and numAttempts < 3:
        uName = input("username >>  ")
        pWord = input("password >>  ")
        if uName in users and pWord in users[uName].values():
            validUser = True
            return True, users[uName]['fName']
        else:
            numAttempts += 1

    if uName in users:
        users[uName]['valid'] = "INVALID"
#NEED None ADDING TO THIS RETURN…
    return False, None
#end def-----------------

def runMenu():
    choice = ' '
    choices = [None , addUser, changePassword, viewUserDetails, listAllUsers] ## sneaky bit of magic python. This is a list of function names...
    while choice != '5':
        choice = displayMenu()
# THE IF STATEMENT NEEDS TO BE INSIDE THE WHLE LOOP…
        if '1' <= choice <='4': choices[int(choice)]() ## calls the function at the relevant position on the list (just add the round brackets)
        elif choice != '5': print("input not recognised. please try again...")
#end def-----------------

def displayMenu():
    print("\nWhat would you like to do:")
    options = ["add user", "change password", "view user details", "list all users", "quit"]
    optionString = ""
    for k, option in enumerate(options):
        optionString += str(k+1) + ") " + option + "\n"
    return input(optionString + ">>  ").strip()[0]
#end def-----------------

def addUser():
    global allLines, Uname, fname, sname
    print("adding user......")

    Uname = input("Please enter a username >>>")
    Npwd = print(createPassword(3))
    fname = input("Please enter your first name >>>")
    sname = input("Please enter your second name >>>")
    users[Uname] = {'pwd':Npwd, 'fName':fname, 'sName':sname}

    with open("UserDetails.txt", 'w') as dFile:
        for k, v in users.items():
            dFile.write(k + " | ")
            for innerKey, detail in v.items():
                dFile.write(innerKey + " | " + detail + " | ")
            dFile.write("\n")


#end def-----------------

def changePassword():
    print("changing Password......")
Reply


Forum Jump:

User Panel Messages

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