[Python] Why doesn't this code generate the random key?

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    Hi guys this a code I followed off a youtube video below this is the link https://youtu.be/IArt2Fgv644?si=UOly57utA04DBSyJ, what did I do wrong? after running it says process finished with exit code 0. but the random module is not working i'm trying to create a random key generator on Pycharm i'm new to coding.I currently have Python version 3.12.5 due to my current operating system windows 8.1 yes, lol.

    import random

    class Key:

    def __init__(self, key= ''):
    if key == '':
    self.key= self.generate()
    else:
    self.key = key.lower()

    def verify(self):
    score = 0
    check_digit = self.key[0]
    check_digit_count = 0
    chunks = self.key.split('-')
    for chunk in chunks:
    if len(chunk) != 4:
    return False
    for char in chunk:
    if char == check_digit:
    check_digit_count += 1
    score += ord(char)
    if score == 1772 and check_digit_count == 3:
    return True
    return False

    def generate(self):
    key = ''
    chunk = ''
    check_digit_count = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890'
    while True:
    while len(key) < 25:
    char = random.choice(alphabet)
    key += char
    if len(chunk) == 4:
    key += '-'
    chunk = ''
    key = key[:-1]
    if Key(key).verify():
    return key
    else:
    key = ''

    def __str__(self):
    valid = 'invalid'
    if self.verify():
    valid = 'Valid'
    return self.key.upper() + ':' + valid

    key = Key('aaaa-bbbb-cccc-dddd-1111')
    print(Key())

    Continue reading...

Compartilhe esta Página