简体   繁体   English

如何使用在它之外的for循环中定义的变量?

[英]How to use variable defined in for-loop outside of it?

How can I use this u_name and u_pass outside this for loop?如何在这个for循环之外使用这个u_nameu_pass

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()

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您可以创建另外两个列表,例如name_listpass_list ,并在 for-loop 之前和 for 循环内部初始化它们使用name_list.append(user[1])pass_list.append(user[2]) ,在 for-loop 使用 index 之后获取每个名称并通过

# 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.在这个 for 循环之外, name_list[0]将引用第一个值, name_list[1]将引用第二个值,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM