首页 > 解决方案 > 当 if 语句为真时显示 elif 语句 显示语句超过 1 次

问题描述

我的项目有问题,看看我的代码

import subprocess
a=input("WANT SPECIFIC PASSWORD OR OF ALL if all type all if specific type wifi name")
if(a=="all"):
    data=subprocess.check_output(['netsh','wlan','show','profiles']).decode('utf-8').split('\n')
    wifis=[line.split(':')[1][1:-1] for  line in data if"All User Profile"in line]
    for wifi in wifis:
        resilt=subprocess.check_output(['netsh','wlan','show','profile',wifi,'key=clear']).decode('utf-8').split('\n')
        resilt=[line.split(':')[1][1:-1]for line in resilt if "Key Content" in line]
        try:
                print('Name',':',wifi,'Password:'+resilt[0])
        except IndexError:
                print('Name',':',wifi,'Password : Cannot be read!')
else:
    data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
    wifis = [line.split(':')[1][1:-1] for line in data if "All User Profile" in line]
    for wifi in wifis:
        resilt = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', wifi, 'key=clear']).decode('utf-8').split(
            '\n')
        resilt = [line.split(':')[1][1:-1] for line in resilt if "Key Content" in line]
        if (a in wifi):
            # print('Name :', fg, "\n"'Password:' + resilt[0])
            try:
                print('Name :',a,"\n"'Password:' + resilt[0])
            except IndexError:
                print('Name', ':', a, 'Password zz: There is no password!')

        elif (a not in wifi):
            print("There is no wifi name",a,"in your system")

在这里我的问题是,当我输入所有内容以获取我电脑中的所有 wifi 密码时,它工作正常,但是当我提供特定的 wifi 名称以获取它的密码时,它会给出但是。请看照片 在此处输入图像描述

这里显示密码,但也显示“您的系统中没有 wifi 名称 Priyanshu”在这里我给出了正确的名称。我只希望它只显示名称和密码而不显示任何其他内容和声明“没有wifi........”它显示了6次而不是一次。我只想要名称和密码当我输入错误的密码时,它显示的语句不只是6次只有一次。我只想要一次。 在此处输入图像描述

我只是对我必须做什么感到困惑。

标签: pythonpython-3.xpython-2.7passwordswifi

解决方案


代码是正确的,但这是您要求的自定义版本

for wifi in wifis:
        resilt = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', wifi, 'key=clear']).decode('utf-8').split(
            '\n')
        resilt = [line.split(':')[1][1:-1] for line in resilt if "Key Content" in line]
        if (a in wifi):
            # print('Name :', fg, "\n"'Password:' + resilt[0])
            try:
                print('Name :',a,"\n"'Password:' + resilt[0])
            except IndexError:
                print('Name', ':', a, 'Password zz: There is no password!')
            break
else :
        print("There is no wifi name",a,"in your system")

说明:如果找到具有所需名称的 wifi,则执行 if 块中的代码。它还执行终止循环的中断命令。最后一个 else 语句仅在 for 循环完成后终止时才执行。因此,如果找不到所需的 wifi,它将执行。但是,如果找到所需的 wifi,则 break 命令将跳过 else 部分。


推荐阅读