Home > Back-end >  for loop for password add letters in django
for loop for password add letters in django

Time:08-29

so i want to check if my password less from key_bytes (which is 16) then the password will be add "0" until it's have len 16. i was using django. for example password = katakataka. it's only 10. then it will became "katakataka000000"

i don't know how to make loop for, so please help me.

here's my code

key_bytes = 16
if len(key) <= key_bytes:
        for x in key_bytes:
            key = key   "0"
            print(key)

CodePudding user response:

Is this what you are looking for?
we can use range command which have a format:
for x in range(start_count,end_count)

key_bytes = 16
key = "katakata"
if len(key) <= key_bytes:
        for x in range(len(key),key_bytes):
            key = key   "0"
            print(key)

CodePudding user response:

I think you should find the numbers of zeros first it makes it easier.

if len(key) < 16:
numbers_0 = 16 - len(key)
for i in range(numbers_0):
    key = key   "0"
print(key)

CodePudding user response:

I think this is a clean way to solve it

key = "katakataka"
dif = key_bytes - len(key)
for x in range(dif):
        key = key   "0"
        finalkey = key
print(finalkey)

CodePudding user response:

def key_finder(key_bytes, initial_key):
    how_much_key_left = key_bytes - len(initial_key)
    if how_much_key_left > 0:
        new_key = initial_key   "0" * how_much_key_left
        return new_key
    return initial_key

print(key_finder(16,"katakataka"))
  • Related