Skip to content

Commit 8d9fc8a

Browse files
authored
Fixed password.py
to raise ValueError when password length is shorter
1 parent 30507bb commit 8d9fc8a

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

‎other/password.py‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,33 @@ def password_generator(length: int = 8) -> str:
2626
# chars_incl= characters that must be in password
2727
# i= how many letters or characters the password length will be
2828
def alternative_password_generator(chars_incl: str, i: int) -> str:
29+
"""
30+
Generate a password that includes the required characters
31+
and has a total length of i.
32+
33+
>>> len(alternative_password_generator("abcdefgh", 8))
34+
8
35+
>>> len(alternative_password_generator("abcdefgh", 5))
36+
Traceback (most recent call last):
37+
...
38+
ValueError: Requested length must be at least the number of required characters.
39+
>>> len(alternative_password_generator("abc", 3))
40+
3
41+
"""
2942
# Password Generator = full boot with random_number, random_letters, and
3043
# random_character FUNCTIONS
3144
# Put your code here...
45+
# chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) +
46+
# random_number(digits, i / 3) + random_characters(punctuation, i / 3)
47+
if i < len(chars_incl):
48+
raise ValueError(
49+
"Requested length must be at least the number of required characters."
50+
)
51+
3252
i -= len(chars_incl)
3353
quotient = i // 3
3454
remainder = i % 3
35-
# chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) +
36-
# random_number(digits, i / 3) + random_characters(punctuation, i / 3)
55+
3756
chars = (
3857
chars_incl
3958
+ random(ascii_letters, quotient + remainder)
@@ -44,8 +63,6 @@ def alternative_password_generator(chars_incl: str, i: int) -> str:
4463
shuffle(list_of_chars)
4564
return "".join(list_of_chars)
4665

47-
# random is a generalised function for letters, characters and numbers
48-
4966

5067
def random(chars_incl: str, i: int) -> str:
5168
return "".join(secrets.choice(chars_incl) for _ in range(i))

0 commit comments

Comments
 (0)