[英]Showing the elif statement when the if statement is true showing the statements more than 1 time
我的项目有问题,请查看我的代码
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 块中的代码。 它还执行终止循环的 break 命令。 最后一个 else 语句仅在 for 循环在完成执行后终止时才执行。 因此,如果未找到所需的 wifi,它将执行。 但是,如果找到所需的 wifi,则 break 命令将跳过 else 部分。
你有一个 for 循环,在它里面,你有 if 和 elif 语句。 所以它发现它会打印它,如果没有,它会打印消息,(就像你在输出的第一个屏幕截图中看到的那样)。
如果没有这样的wifi,它仍然会打印它。
我认为没有问题,脚本可以正常工作。
如果您找到当前的 wifi,您也可以中断循环,最好不要打印“未找到 wifi...”...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.