python - 我怎么能不重复猜测?
问题描述
我怎样才能在不重复猜测的情况下暴力破解密码,也不导入其他任何东西?这是我到目前为止的代码
import random
import string
guessAttempts = 0
myPassword = input("Enter a password for the computer to try and guess: ")
passwordLength = len(myPassword)
while True:
guessAttempts = guessAttempts + 1
passwordGuess = ''.join([random.choice(string.ascii_letters + string.digits)for n in range(passwordLength)])
if passwordGuess == myPassword:
print(passwordGuess)
print("Password guessed successfully!")
print("It took the computer %s guesses to guess your password." % (guessAttempts))
break
任何帮助,将不胜感激
解决方案
取 的n
-way 产品string.ascii_letters + string.digits
并对其进行迭代。
from itertools import product
passwords = map(''.join, product(string.ascii_letters + string.digits, repeat=n))
for (guessAttempts, passwordGuess) in enumerate(passwords, start=1):
...
itertools
位于标准库中,因此无论您选择使用与否,它都已安装:您不妨使用它。
推荐阅读
- android - Dart/Flutter 图像库解码位图失败
- java - 我的 Java 代码无法运行,我该如何调试它?
- python - tf.stop_gradient() 是否有助于记忆?
- vue.js - 在 vue.js 中选择选项后,1 方式绑定失败
- scala - 不稳定的 Spark 应用程序
- javascript - 如何修复解析错误意外令牌
- python - 自定义 QTreeWidgetItem Pyside2
- android - Delphi Android Firebase Cloud Messaging:收到通知时应用程序崩溃
- python - Python - 保存时设置 TIFF 图像分辨率的枕头
- typescript - 是否有在 TypeScript 中使用嵌套类作为类型的方法