我是 Python 新手(2 周),課外一直在研究隨機密碼生成器。我已經制作了密碼生成器;但是,我不確定如何開始為用戶生成另一個密碼的程序。到目前為止,我已經嘗試過創建一個回圈,但很快就迷路了。關于我做錯了什么,我最好的猜測是我只是不太了解回圈,而創建一個回圈將解決我的問題。
# This greets the user
print('Hello, Welcome to this random password generator')
# This asks the user for the length of the password
length = int(input('\nEnter the length of the password you would like to generate: '))
# This defines data for the string module
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
# This combines the data
all = lower upper number
# This allows us to randomize the data collected
temp = random.sample(all, length)
# This generates the password
password = "".join(temp)
# This will generate a password of up to 94 characters
print(password)
# This prompts the user to generate another password
next_generation = input("Would you like to generate another password? (yes/no): ")
uj5u.com熱心網友回復:
import random
import string
while True:
length = int(input("Enter password length: "))
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
all = lower upper number
temp = random.sample(all,length)
password = ''.join(temp)
print(password)
# **** This block asks user whether to create another password ****
# If Yes, code loops back to the beginning and does it again
# If No, code stops.
another = input("Generate another password? Y/N: ").capitalize().strip()
if another == 'Y':
another = True
continue
else:
break
uj5u.com熱心網友回復:
您確實需要使用回圈并將資料存盤在陣列中或在回圈中列印。這篇文章可能會有所幫助,因為它解釋了: 如何使用回圈來保持程式運行
我假設您希望您的程式每次都不斷要求輸入新密碼,并且密碼的數量不固定。否則你會使用for loop.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410648.html
標籤:
上一篇:如何將DjangoNginx應用程式與HTTP和HTTPS請求一起使用?
下一篇:為什么java-XX: PrintFlagsFinal和jinfo-flagMaxHeapSize的MaxHeapSize值不同
