Als erste kleine eigene Übung habe ich dann den folgenden Code produziert. Funktioniert auch soweit wie ich es mir vorgestellt habe, allerdings gehe ich davon aus, das noch viele stilistische Fehler enthalten sind, oder eben andere Fehler.
Ich denke ich brauche nicht erklären was das Script macht
Code: Alles auswählen
import random
class GeneratePassword(object):
"""
Generates a random password.
Attribute : with_special_character, password_lenth
"""
def __init__(self, with_special_character = False, password_lenth = 8):
self.password_lenth = password_lenth
self.with_special_character = with_special_character
self.password = ""
characters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
special_characters = ['!', '@', '#', '$', '%', '^', '&', '*', '?']
def make_password(self):
"""
create the random password
"""
random_range = 3
if self.with_special_character:
random_range = 4
for i in range(self.password_lenth):
character_kind = random.randrange(0, random_range)
if character_kind == 0:
self.password += self.characters[random.randrange(0, 24)].lower()
elif character_kind == 1:
self.password += self.characters[random.randrange(0, 24)]
elif character_kind == 2:
self.password += str(random.randrange(0,10))
elif character_kind == 3:
self.password += self.special_characters[random.randrange(0, 9)]
def main():
for i in range(10):
new_password = GeneratePassword()
new_password.make_password()
print new_password.password
if __name__ == "__main__":
main()
Aber irgendwie muss man das ja üben
Freue mich über jede konstruktive Kritik und alles was mir hilft irgendwann einmal ein guter Programmierer zu sein
Mit freundlichem Gruß