P ython p rogramming e xercises


E X E R C I S E # 1 9 : P A S S W O R D G E N E R A T O R



Yüklə 1,51 Mb.
Pdf görüntüsü
səhifə49/124
tarix14.05.2023
ölçüsü1,51 Mb.
#113537
1   ...   45   46   47   48   49   50   51   52   ...   124
PythonProgrammingExercisesGentlyExplained

E X E R C I S E # 1 9 : P A S S W O R D G E N E R A T O R
generatePassword(12)
→ 
'v*f6uoklQJ!d' 
generatePassword(12)
→ 
' Yzkr(j2T$MsG' 
generatePassword(16)
→ 
' UVp7ow8T%5LZl1la' 
While a password made from a single English word like ―rosebud‖ or ―swordfish‖ is easy to 
remember, it isn’t secure. A dictionary attack is when hackers program their computers to repeatedly try 
logging in with every word in the dictionary as the password. A dictionary attack won’t work if you 
use randomly generated passwords. They may not be easy to remember, but they make hacking your 
accounts more difficult. 
Exercise Description 
Write a generatePassword() function that has a length parameter. The length 
parameter is an integer of how many characters the generated password should have. For security 
reasons, if length is less than 12, the function forcibly sets it to 12 characters anyway. The password 
string returned by the function must have at least one lowercase letter, one uppercase letter, one 
number, and one special character. The special characters for this exercise are ~!@#$%^&*()_+.
Your solution should import Python’s random module to help randomly generate these 
passwords. 
These Python assert statements stop the program if their condition is False. Copy them to 
the bottom of your solution program. Your solution is correct if the following assert statements’ 
conditions are all True: 
assert len(generatePassword(8)) == 12 
pw = generatePassword(14) 
assert len(pw) == 14 
hasLowercase = False 
hasUppercase = False 
hasNumber = False 
hasSpecial = False 
for character in pw: 
if character in LOWER_LETTERS: 
hasLowercase = True 
if character in UPPER_LETTERS: 


Python Programming Exercises, Gently Explained 
59 
hasUppercase = True 
if character in NUMBERS: 
hasNumber = True 
if character in SPECIAL: 
hasSpecial = True 
assert hasLowercase and hasUppercase and hasNumber and hasSpecial 
Try to write a solution based on the information in this description. If you still have trouble 
solving this exercise, read the Solution Design and Special Cases and Gotchas sections for 
additional hints. 
Prerequisite concepts: import statements, random module, strings, string concatenation, 
len()
, append(), randint(), shuffle(), join() 

Yüklə 1,51 Mb.

Dostları ilə paylaş:
1   ...   45   46   47   48   49   50   51   52   ...   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2025
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin