Apr-18-2018, 01:39 PM
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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......" ) |