python - 当 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次只有一次。我只想要一次。
我只是对我必须做什么感到困惑。
解决方案
代码是正确的,但这是您要求的自定义版本
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 部分。
推荐阅读
- mysql - Laravel - 如何立即更新表记录保存在其中
- java - Selenium:每当有新聊天可用时,如何单击聊天页面中的元素
- eof - 嗨,我收到 EOF 错误,我该如何纠正?
- r - 在箱线图中指定点符号的plotly问题
- scala - Scala 转换为泛型类型
- mysql - 如何在 MySql docker 镜像中安装 sudo 和 nano 命令
- reactjs - 在 ReactJs 中将数据从状态发送到组件
- c++ - MFC C++ 通过 WM_DEVICECHANGE 通知区分两个设备
- bdd - 如果满足特定条件,有没有办法在空手道中多次运行特定场景?
- css - .ag-cell 菜单背景颜色(Angular)