0

How can I use this u_name and u_pass outside this for loop?

cursr.execute("SELECT rowid ,* FROM usernameandpassword")
user_and_pass = (cursr.fetchall())

for users in user_and_pass:
    global u_name
    global u_pass
    u_name = (users[1])
    u_pass = (users[2])

    if uni_username.get() == u_name and uni_pass.get() == u_pass:
        show_new_user_window()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    u_name and u_pass are still in the namespace when the for loop exits, but they'll be the last entries in the ```user_and_pass``` list. are you trying to keep a list of usernames and passwords? – EMiller Nov 30 '20 at 16:38
  • yes , because if i use u_name and u_pass outside for loop , I can get only last entry – AADITYA JAIN Nov 30 '20 at 16:40
  • [Global variables are _evil_!](//stackoverflow.com/q/19158339/843953) You can almost always replace global variables with something else to get the same behavior without all the pain that global variables bring. – Pranav Hosangadi Nov 30 '20 at 16:55
  • Those variables are available outside the loop. – juanpa.arrivillaga Nov 30 '20 at 17:10

1 Answers1

0

you could create another two lists like name_list and pass_list, and initialize them before for-loop and inside for loop use name_list.append(user[1]), pass_list.append(user[2]), after for-loop use index to get each name and pass

# intialize name_list and pass_list lists
name_list = []
pass_list = []
for users in user_and_pass:
    # save them into our lists
    name_list.append(users[1])
    pass_list.append(users[2])

outside this for loop, name_list[0] will refer to the first value, name_list[1] will refer to second value and so on.

meng luo
  • 21
  • 6
  • what do you mean by that 'after for-loop use index to get each name and pass' can you explain , because I am beginner I don't know too much – AADITYA JAIN Nov 30 '20 at 16:50