如何在不重复猜测的情况下,也不导入任何其他内容的情况下强制输入密码?到目前为止这是我的代码
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
的string.ascii_letters + string.digits
路积并迭代它itertools
在标准库中,因此无论您是否选择使用它,它都已经安装好了:您也可以使用它相关问题 更多 >
编程相关推荐