Skip to main content
 首页 » 编程设计

Python之if 和 else 在 for 循环中,计数不起作用

2024年10月24日13daizhj

这是一个简单的登录程序 - 逻辑在 VB.Net 中有效,但我无法将其翻译成 Python。 Python 代码不起作用,但是两个用户(un_lucky 和 ​​Jerry)在 Stackoverflow 上提供的 VB.Net 代码可以。任何人都可以为我指出 python 代码的修复吗?它基本上会产生一系列“拒绝访问”,而不仅仅是一个,当密码不正确时,如果用户名或密码是数组中的第二个,那么它会产生一个访问被拒绝,然后是一个访问被授予。

count=0 
for counter in range(0,len(fields)): 
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]: 
        count=count+1 
 
        if count > 0: 
            welcome=Label(myGui,text="Access Granted. Loading Profile ....") 
            welcome.pack() 
    else: 
                denied=Label(myGui,text="Access Denied") 
                denied.pack() 

这里是 VB.net 代码(逻辑),除了上面的 python 程序是从文本文件读取之外,它确实可以实现几乎相同的事情。

它现在可以完美运行(几乎)但是用户名和密码中的空白条目也会产生“已授予访问权限”...无法弄清楚为什么! 完整代码如下:

def verifylogin(): 
 
fin=open("moosebook.txt","r") 
data=fin.readlines() 
for line in data: 
    fields=line.split() 
    fields=[i.rstrip("','") for i in fields] #strips the named character from END of field 
    fields=[i.replace("'",'') for i in fields]#when reading the list, you want to remoe the ',' so it isn't part of the username or password 
    fields=[i.replace("(",'') for i in fields] #simiarly, remove the bracket and replace it 
    fields=[i.replace(")",'') for i in fields] #simiarly, remove the bracket and replace it 
 
    line=line.rstrip() 
    print(fields) 
 
flag=0 
for counter in range(0,len(fields)): 
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]: 
        flag=flag+1 
 
if flag>0: 
            welcome=Label(myGui,text="Access Granted. Loading Profile ....") 
            welcome.pack() 
else: 
                denied=Label(myGui,text="Access Denied") 
                denied.pack() 

请您参考如下方法:

缩进在 Python 中很重要。您将 if 测试放在循环中。您需要删除多余的缩进以将其放在循环之外:

for counter in range(0,len(fields)): 
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]: 
        count=count+1 
 
if count > 0: 
    welcome=Label(myGui,text="Access Granted. Loading Profile ....") 
    welcome.pack() 
else: 
    denied=Label(myGui,text="Access Denied") 
    denied.pack() 

请注意 if 和 else 行现在如何与 for 行从同一列开始。

因为在您的代码中,if 语句被缩进到更深层次(与检查密码的 if 测试相同)您多次创建了标签,一次对于每次循环运行。

缩进用于分隔代码块的方式与 VB.NET 代码使用 Next 和 End If 明确分隔循环和条件测试的方式相同。

循环可能会被清理;只调用一次 textlogin.get() 和 textpassword.get(),并使用 zip() 配对字段值,以及any()查看是否有第一个匹配项(这里就足够了):

login, pw = textlogin.get(), textpassword.get() 
if any(pair == (login, pw) for pair in zip(fields, fields[1:])): 
    welcome=Label(myGui,text="Access Granted. Loading Profile ....") 
    welcome.pack() 
else: 
    denied=Label(myGui,text="Access Denied") 
    denied.pack() 

如果您打算将字段作为对(而不是作为滑动窗口)循环,则使用:

login, pw = textlogin.get(), textpassword.get() 
fields_iter = iter(fields) 
if any(pair == (login, pw) for pair in zip(fields_iter, fields_iter)): 
    welcome=Label(myGui,text="Access Granted. Loading Profile ....") 
    welcome.pack() 
else: 
    denied=Label(myGui,text="Access Denied") 
    denied.pack()