Simple username and password application in Python -


i'm trying build simple login , password application using dictionary. works fine except part checks if login matches password (in bottom says "login successful!").

if create login 'a' , password 'b', , create login 'b' , password 'a', log me in if tried log in login 'a' , password 'a'. checks if characters exist somewhere in dictionary, not if pair.

any suggestions how fix this?

users = {} status = ""  while status != "q":     status = raw_input("are registered user? y/n? press q quit: ")        if status == "n": #create new login          createlogin = raw_input("create login name: ")           if createlogin in users: # check if login name exist in dictionary              print "login name exist!\n"          else:              createpassw = raw_input("create password: ")              users[createlogin] = createpassw # add login , password              print("\nuser created!\n")           elif status == "y": #login user         login = raw_input("enter login name: ")          if login in users:            passw = raw_input("enter password: ")            print             if login in users , passw in users: # login matches password                print "login successful!\n"          else:             print             print("user doesn't exist!\n") 

edit

now working, i'm trying divide application 3 functions, readability purposes. works, except infinite loop.

any suggestions why?

users = {} status = ""  def displaymenu():     status = raw_input("are registered user? y/n? press q quit: ")       if status == "y":         olduser()     elif status == "n":         newuser()  def newuser():     createlogin = raw_input("create login name: ")      if createlogin in users: # check if login name exists         print "\nlogin name exist!\n"     else:         createpassw = raw_input("create password: ")         users[createlogin] = createpassw # add login , password         print("\nuser created!\n")       def olduser():     login = raw_input("enter login name: ")     passw = raw_input("enter password: ")      # check if user exists , login matches password     if login in users , users[login] == passw:          print "\nlogin successful!\n"     else:         print "\nuser doesn't exist or wrong password!\n"  while status != "q":                 displaymenu() 

right checking if given password, passw, matches keys in users (not right). need see if password entered matches particular user's password. since have checked if username exists in dictionary's keys don't have check again, try like:

if passw == users[login]:     print "login successful!\n" 

edit:

for updated code, i'm going assume "infinite loop" mean cannot use q exit program. it's because when you're inside displaymenu, save user input in local variable named status. local variable does not refer same status checking,

while status != "q":  

in other words, using variable status in two different scopes (changing inner scope not change outer).

there many ways fix this, 1 of changing,

while status != "q":     status = displaymenu() 

and adding return statement @ end of displaymenu so,

return status 

by doing this, saving new value of status local scope of displaymenu global scope of script while loop can work properly.

another way add line beginning of displaymenu,

global status 

this tells python status within displaymenu refers global scoped status variable , not new local scoped one.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -